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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top