This question already has answers here:

Linq for 2 collection simultaneously
(3个答案)
所以我有一些代码
        List<ParameterInfo> theseParams = this.Action.GetParameters().OrderBy(p => p.Name).ToList(),
                            otherParams = other.Action.GetParameters().OrderBy(p => p.Name).ToList();
        if(theseParams.Count != otherParams.Count)
            return false;
        for(int i = 0; i < theseParams.Count; ++i)
        {
            ParameterInfo thisParam = theseParams[i],
                          otherParam = otherParams[i];
            if(thisParam.Name != otherParam.Name)
                return false;
        }
        return true;

我想知道是否有一种简洁的方法可以同时遍历列表?

最佳答案

当然,只要使用Enumerable.ZipEnumerable.All

return theseParams.Count == otherParams.Count
    && theseParams.Zip(otherParams, (t,o) => new {These = t, Other =o})
    .All(x => x.These.Name == x.Other.Name);

10-07 23:17