如果我想要两个列表中的公共元素,我可以使用 intersect 函数:
var listC = listA.Intersect(listB);
但这比较对象。如果列表具有 Persons 类型的对象,并且我想获取具有相同名称的人员,例如,我该怎么做?我在哪里设置名称属性的条件?
谢谢。
将其传递给自定义IEqualityComparer<T>
。
首先,创建一个实现该接口的类:
public class PersonNameEqualityComparer:IEqualityComparer<Person>
{
public int GetHashCode (Person obj)
{
return obj.Name.GetHashcode ();
}
public bool Equals (Person x, Person y)
{
return x.Name == y.Name;
}
}
然后,您需要做的就是将其实例传递IEqualityComparer
给 intersect 方法。
var result = listA.Intersect(listB, new PersonNameEqualityComparer());
您可以使用泛型和 lambda 将其扩展到任何对象和任何属性:
public class PropertyEqualityComparer<TObject, TProperty> : IEqualityComparer<TObject>
{
Func<TObject, TProperty> _selector;
IEqualityComparer<TProperty> _internalComparer;
public PropertyEqualityComparer(Func<TObject, TProperty> propertySelector, IEqualityComparer<TProperty> innerEqualityComparer = null)
{
_selector = propertySelector;
_internalComparer = innerEqualityComparer;
}
public int GetHashCode(TObject obj)
{
return _selector(obj).GetHashCode();
}
public bool Equals(TObject x, TObject y)
{
IEqualityComparer<TProperty> comparer = _internalComparer ?? EqualityComparer<TProperty>.Default;
return comparer.Equals(_selector(x), _selector(y));
}
}
然后你可以像这样使用它:
var result = listA.Intersect(listB, new PropertyEqualityComparer<Person, string>(p => p.Name));
或像这样:
var result = listA.Intersect(listB, new PropertyEqualityComparer<Person, string>(p => p.Age));
等等。