patterncsharpCritical
Distinct() with lambda?
Viewed 0 times
distinctwithlambda
Problem
Right, so I have an enumerable and wish to get distinct values from it.
Using
Well and good, but if I have an enumerable of objects for which I need to specify equality, the only available overload is:
The equality comparer argument must be an instance of
What I would have expected is an overload that would take a lambda, say a
Anyone know if some such extension exists, or some equivalent workaround? Or am I missing something?
Alternatively, is there a way of specifying an
Update
I found a reply by Anders Hejlsberg to a post in an MSDN forum on this subject. He says:
The problem you're going to run into is that when two objects compare
equal they must have the same GetHashCode return value (or else the
hash table used internally by Distinct will not function correctly).
We use IEqualityComparer because it packages compatible
implementations of Equals and GetHashCode into a single interface.
I suppose that makes sense.
Using
System.Linq, there's, of course, an extension method called Distinct. In the simple case, it can be used with no parameters, like:var distinctValues = myStringList.Distinct();Well and good, but if I have an enumerable of objects for which I need to specify equality, the only available overload is:
var distinctValues = myCustomerList.Distinct(someEqualityComparer);The equality comparer argument must be an instance of
IEqualityComparer. I can do this, of course, but it's somewhat verbose and, well, cludgy.What I would have expected is an overload that would take a lambda, say a
Func:var distinctValues = myCustomerList.Distinct((c1, c2) => c1.CustomerId == c2.CustomerId);Anyone know if some such extension exists, or some equivalent workaround? Or am I missing something?
Alternatively, is there a way of specifying an
IEqualityComparer inline (embarrass me)?Update
I found a reply by Anders Hejlsberg to a post in an MSDN forum on this subject. He says:
The problem you're going to run into is that when two objects compare
equal they must have the same GetHashCode return value (or else the
hash table used internally by Distinct will not function correctly).
We use IEqualityComparer because it packages compatible
implementations of Equals and GetHashCode into a single interface.
I suppose that makes sense.
Solution
IEnumerable filteredList = originalList
.GroupBy(customer => customer.CustomerId)
.Select(group => group.First());Code Snippets
IEnumerable<Customer> filteredList = originalList
.GroupBy(customer => customer.CustomerId)
.Select(group => group.First());Context
Stack Overflow Q#1300088, score: 1130
Revisions (0)
No revisions yet.