泛型List去除重复指定字段ID

var list=listTemp.Distinct(new IDComparer ()).ToList();

重写比较的方法:

public class IDComparer : IEqualityComparer<T>
{
public bool Equals(T x, T  y)
{
if (x == null)
return y == null;
return x.ID == y.ID;
}

public int GetHashCode(T obj)
{
if (obj == null)
return 0;
return obj.ID.GetHashCode();
}
}

 

参考链接:http://blog.csdn.net/yang651280121/article/details/8259097

 

posted on 2017-11-28 19:07  xs~ing  阅读(1161)  评论(0编辑  收藏  举报