Tag Archives: linq

LINQ Distinct Fix

21 Dec

I needed to use the LINQ Distinct operation on a list of objects today and I ran into the fact that Distinct doesn’t take a lambda expression. You have to create a class that inherits IEqualityComparer and implements both the Equals and GetHashCode methods. This seems like a real pain to me. Distinct should take a lambda expression like the rest of the LINQ operators.

I found a workaround by using GroupBy, First, and Select like so.

var distinct = collection
       .GroupBy(c => c.FilterProperty)
       .Select(c => c.First())