為了去重復,寫了一個通用的比較容器類,可以用在需要比較的地方,且支持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();