LINQ: Enhancing Distinct With The SelectorEqualityComparer
On my last post, I introduced the PredicateEqualityComparer and a Distinct extension method that receives a predicate to internally create a PredicateEqualityComparer to filter elements.
Using the predicate, greatly improves readability, conciseness and expressiveness of the queries, but it can be even better. Most of the times, we don’t want to provide a comparison method but just to extract the comaprison key for the elements.
So, I developed a SelectorEqualityComparer that takes a method that extracts the key value for each element. Something like this:
public class SelectorEqualityComparer<TSource, Tkey> : EqualityComparer<TSource> where Tkey : IEquatable<Tkey> { private Func<TSource, Tkey> selector;<span style="color: blue">public </span>SelectorEqualityComparer(<span style="color: #2b91af">Func</span><TSource, Tkey> selector) : <span style="color: blue">base</span>() { <span style="color: blue">this</span>.selector = selector; } <span style="color: blue">public override bool </span>Equals(TSource x, TSource y) { Tkey xKey = <span style="color: blue">this</span>.GetKey(x); Tkey yKey = <span style="color: blue">this</span>.GetKey(y); <span style="color: blue">if </span>(xKey != <span style="color: blue">null</span>) { <span style="color: blue">return </span>((yKey != <span style="color: blue">null</span>) && xKey.Equals(yKey)); } <span style="color: blue">return </span>(yKey == <span style="color: blue">null</span>); } <span style="color: blue">public override int </span>GetHashCode(TSource obj) { Tkey key = <span style="color: blue">this</span>.GetKey(obj); <span style="color: blue">return </span>(key == <span style="color: blue">null</span>) ? 0 : key.GetHashCode(); } <span style="color: blue">public override bool </span>Equals(<span style="color: blue">object </span>obj) { <span style="color: #2b91af">SelectorEqualityComparer</span><TSource, Tkey> comparer = obj <span style="color: blue">as </span><span style="color: #2b91af">SelectorEqualityComparer</span><TSource, Tkey>; <span style="color: blue">return </span>(comparer != <span style="color: blue">null</span>); } <span style="color: blue">public override int </span>GetHashCode() { <span style="color: blue">return base</span>.GetType().Name.GetHashCode(); } <span style="color: blue">private </span>Tkey GetKey(TSource obj) { <span style="color: blue">return </span>(obj == <span style="color: blue">null</span>) ? (Tkey)(<span style="color: blue">object</span>)<span style="color: blue">null </span>: <span style="color: blue">this</span>.selector(obj); }
}
Now I can write code like this:
.Distinct(new SelectorEqualityComparer<Source, Key>(x => x.Field))
And, for improved readability, conciseness and expressiveness and support for anonymous types the corresponding Distinct extension method:
public static IEnumerable<TSource> Distinct<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector) where TKey : IEquatable<TKey> { return source.Distinct(new SelectorEqualityComparer<TSource, TKey>(selector)); }
And the query is now written like this:
.Distinct(x => x.Field)
For most usages, it’s simpler than using a predicate.