我想知道是否可以使用 lambda 表达式来解决这个问题:
List<Foo> listOne = service.GetListOne();
List<Foo> listTwo = service.GetListTwo();
List<Foo> result = new List<Foo>();
foreach(var one in listOne)
{
foreach(var two in listTwo)
{
if((one.Id == two.Id) && one.someKey != two.someKey)
result.Add(one);
}
}
你当然可以!您可以使用这种超载 http://msdn.microsoft.com/en-us/library/bb355408.aspx林克的Intersect
扩展方法需要一个IEqualityComparer<T>
, 像这样:
public class FooComparer : IEqualityComparer<Foo>
{
public bool Equals(Foo x, Foo y)
{
return x.Id == y.Id && x.someKey != y.someKey;
}
public int GetHashCode(Foo x)
{
return x.Id.GetHashCode();
}
}
...
var comparer = new FooComparer();
List<Foo> listOne = service.GetListOne();
List<Foo> listTwo = service.GetListTwo();
List<Foo> result = listOne.Intersect(listTwo, comparer).ToList();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)