Question

How does Distinct() work on a List<> of anonymous type? will it just do a property compare? or will it always return the same list?

example:

 List<SomeObject> list;
 ....
 ....

 var result = list
           .Where(i => i.Condition)
           .Select(i => new 
               {
                  Name = i.Name,
                  Date = i.Date
               });
           .Distinct()
           .ToList()  

Please note I applied the distinct on the anonymous-type list.

Was it helpful?

Solution

From MSDN

Because the Equals and GetHashCode methods on anonymous types are defined in terms of the Equals and GetHashcode methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top