博客园  :: 首页  :: 联系 :: 管理

LINQ Distinct 自定义

Posted on 2008-07-28 12:57  sunrack  阅读(3151)  评论(0编辑  收藏  举报

 

 

1、定义对象

        public class BusinessObj
        {
            
public string OtherData;
            
public int Key;
        }

 

2、自定义比较符

        public class BusinessObjComparer : IEqualityComparer<BusinessObj>
        {
            
public bool Equals(BusinessObj source, BusinessObj dest)
            {
                
return source.Key == dest.Key;
            }

            
public int GetHashCode(BusinessObj obj)
            {
                
return obj.Key.GetHashCode();
            }
        }

 

 

 

3、使用

List<BusinessObj> curBusinessObjList = 

curBusinessObjList.FindAll((BusinessObj curBusinessObj) 
=> (curBusinessObj.Key != 1)).Distinct(new BusinessObjComparer()).ToList();