例如:根据Para表中的type和paraid 字段进行比较
public class TypeComparer : IEqualityComparer<Para>
{
bool IEqualityComparer<Para>.Equals(Para x,Para y)
{
if (x == null)
return y == null;
return x.type == y.type&&x.paraid ==y.paraid ;//如果有英文,考虑比较大小写
}
int IEqualityComparer<Para>.GetHashCode(Para obj)
{
if (obj == null)
return 0;
return obj.type.GetHashCode() ^ obj.paraid .GetHashCode();
}
}
注意:多条件返回bool用&&连接,返回int用^连接!
在ef中的用法:
List<Para> listPara = _pService.LoadEntities(p => p.flag == 1).Select(pa => new Para() { type = pa.type, paraid = pa.paraid }).Distinct(new TypeComparer()).Select(pa => new Para() { name = pa.type, value = pa.paraid }).ToList();