class ProductComparare : IEqualityComparer
{
private Func _funcDistinct;
public ProductComparare(Func funcDistinct)
{
this._funcDistinct = funcDistinct;
}
public bool Equals(YEWULIANG x, YEWULIANG y)
{
return _funcDistinct(x).Equals(_funcDistinct(y));
}
public int GetHashCode(YEWULIANG obj) { return this._funcDistinct(obj).GetHashCode(); }
}
使用如下:
List list=new List (){
new YEWULIANG {name="1",age=12} ,
new YEWULIANG {name="1",age=13} ,
new YEWULIANG {name="2",age=14} ,
}
//使用后
var listCNAME = list.Distinct(new ProductComparare(m => m.c_name)).ToList();
listCNAME里面的对象为
new YEWULIANG {name="1",age=12} ,
new YEWULIANG {name="2",age=14} ,