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.

Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top