Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top