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} ,

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM