Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top