你的位置:首页 > ASP.net教程

[ASP.net教程]List去重复(List中存的是对象)


class ProductComparare : IEqualityComparer<YEWULIANG>    {      private Func<YEWULIANG, object> _funcDistinct;      public ProductComparare(Func<YEWULIANG, object> 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<YEWULIANG> list=new List<YEWULIANG> (){

  new YEWULIANG {name="1",age=12} ,

  new YEWULIANG {name="1",age=13} ,

  new YEWULIANG {name="2",age=14} ,

}

//使用后

var listCNAME = list.Distinct<YEWULIANG>(new ProductComparare(m => m.c_name)).ToList();

listCNAME里面的对象为

 new YEWULIANG {name="1",age=12} ,

   new YEWULIANG {name="2",age=14} ,