为了去重复,写了一个通用的比较容器类,可以用在需要比较的地方,且支持Lamda表达式,代码如下:
public class DataComparer<T>:IEqualityComparer<T> where T:class { private Func<T, T, bool> _compareFunc; public DataComparer(Func<T,T,bool> compareFunc) { this._compareFunc = compareFunc; } public bool Equals(T x, T y) { return this._compareFunc(x, y); } public int GetHashCode(T obj) { return obj.GetHashCode(); } }
调用如下:
dataTable.AsEnumerable().Distinct(new DataComparer<DataRow>((x, y) => return (x[1] == y[1] && x[2] ==y[2]))).CopyToDataTable();
时间: 2024-10-24 11:37:24