在C#中有没有紧凑而优雅的方法可以同时遍历两个列表?
因此,我有一些代码,我做的东西像在C#中有没有紧凑而优雅的方法可以同时遍历两个列表?,c#,.net,algorithm,linq,C#,.net,Algorithm,Linq,因此,我有一些代码,我做的东西像 List<ParameterInfo> theseParams = this.Action.GetParameters().OrderBy(p => p.Name).ToList(), otherParams = other.Action.GetParameters().OrderBy(p => p.Name).ToList(); if(thesePa
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;
List theseParams=this.Action.GetParameters().OrderBy(p=>p.Name.ToList(),
otherParams=other.Action.GetParameters().OrderBy(p=>p.Name.ToList();
如果(theseParams.Count!=其他Params.Count)
返回false;
对于(int i=0;i
我想知道是否有一种紧凑的方法可以一次遍历列表?当然,只需使用and
当然,只要使用和
签出签出导入注意,这不会处理
如果(theseParams.Count!=otherParams.Count)
检查,则该部分仍需要在代码中。Zip的行为类似于(int i=0;i,这一部分仍然需要在代码中。Zip的行为类似于for的(int i=0;i
@ScottChamberlain遗漏了这一点,现已修复。尽管问题重复,但这个答案值得投票:)
return theseParams.Count == otherParams.Count
&& theseParams.Zip(otherParams, (t,o) => new {These = t, Other =o})
.All(x => x.These.Name == x.Other.Name);