1.实现方法
View Code
public delegate bool EqualsComparer<T>(T x, T y); /// <summary> /// 去除List重复项 /// </summary> /// <typeparam name="T"></typeparam> public class Compare<T> : IEqualityComparer<T> { private EqualsComparer<T> _equalsComparer; public Compare(EqualsComparer<T> equalsComparer) { this._equalsComparer = equalsComparer; } public bool Equals(T x, T y) { if (null != this._equalsComparer) return this._equalsComparer(x, y); else return false; } public int GetHashCode(T obj) { return obj.ToString().GetHashCode(); } }
2.开始去除重复项
List<T> task = List<T>.Distinct(new Compare<Task>((x, y) => (null != x && null != y) && (x.PrimaryKeyTypeID == y.PrimaryKeyTypeID))).ToList();
完成!