System.Linq Distinct method

  • First seen directly .Distinct Distinct certainly be taken for granted () called with.
  • Distinct need to know where the object is to come and go as a unit of weight. When you Select new map of the same time even if property values ​​will not be de-emphasis.
  • Use a single column on a little here by using the 
  • Use multiple columns (for example, I need to re-KeyValuePairModel the object):
     public  class KeyValuePairModel 
        { 
            public  String the Name { GET ; the SET ;}
             public  String Code { GET ; the SET ;} 
        } 
    // here bottom will first determine whether the object is relatively the same object will first call GetHashCode if not the same object will be called Equals method. 
        public  class KeyValuePairRowComparer: the IEqualityComparer <KeyValuePairModel> 
        { 
            public  BOOL Equals (KeyValuePairModel T1, T2 KeyValuePairModel) 
            { 
                return (t1.name == == t2.Name && t1.Code t2.Code); 
            } 
            public  int GetHashCode(KeyValuePairModel t)
            {
                return t.ToString().GetHashCode();
            }
        }

     

  • use:
    arealist.Select(c => new KeyValuePairModel() { Code = c.CityCode, Name = c.CityName }).Distinct(new KeyValuePairRowComparer())

     

Guess you like

Origin www.cnblogs.com/chongyao/p/12357899.html