Answers for "distinct a list of class objects by one attribute"

C#
0

distinct a list of class objects by one attribute

class ItemEqualityComparer : IEqualityComparer<Class>
        {
            public bool Equals(Class x, Class y)
            {
                // Two items are equal if their keys are equal.
                return x.Attribute == y.Attribute;
            }

            public int GetHashCode(Class obj)
            {
                return obj.Attribute.GetHashCode();
            }
        }
        
        // then select distinct in method
        List<Class> returnList = new List<Class>();
        returnList = sourceList.Distinct(new ItemEqualityComparer()).ToList();
Posted by: Guest on November-05-2020

Code answers related to "distinct a list of class objects by one attribute"

C# Answers by Framework

Browse Popular Code Answers by Language