Click here to Skip to main content
15,867,594 members
Articles / All Topics
Technical Blog

C#: LINQ and IEqualityComparer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
19 Apr 2014CPOL9 min read 48K   8   5
When writing programs in C# with the help of LINQ I have come across the IEqualityComparer generic interface several times.
When writing programs in C# with the help of LINQ I have come across the IEqualityComparer<T> generic interface several times. The name conveys the purpose of the interface clearly, still, surprisingly, my first attempts to apply it were marked by strong confusion - the thing merely didn't work the way I wanted it to. Moreover, after going to Google and StackOverflow I have noticed that I'm not the only one to face difficulties with it. After inspecting multiple SO questions and answers related to the topic as well as some other articles I have both found the solution that fitted my needs for the moment and got some understanding of the way IEqualityComparer is actually used by LINQ operators. Here I will try to explain what I got from there.
 
We will deal with the Distinct(..) LINQ extension method, because it seems both the simplest and the most common consumer of IEqualityComparer<T> interface. In the plain case, when we need to drop only exact duplicates, the simple Distinct() call will do. The simplest possible example is getting unique integers from a collection of numbers:
 
var numbers = new int[] { 1, 2, 3, 2, 5, 5, 3 };<br />foreach (var n in numbers.Distinct())<br />{<br />     Console.WriteLine(n);<br />}<br /><br />/*<br />The output is:<br />1<br />2<br />3<br />5<br />*/<br />
However, quite frequently we, C# programmers, use classes and want to get rid of duplicated objects in a collection the same way. Suppose, we do something with actors of a movie:
class MovieActor<br />{<br />    public string LastName { get; set; }<br />    public string FirstName { get; set; }<br />    public string CharacterName { get; set; }<br /><br />    public override string ToString()<br />    {<br />        return String.Format(<br />         "{0} \"{1}\" {2}", FirstName, CharacterName, LastName);<br />    }<br /><br />    public static List<MovieActor> CreateSome()<br />    {<br />        return new List<MovieActor>()<br />        {<br />            new MovieActor() { <br />             FirstName = "Brad", <br />             LastName = "Pitt", <br />             CharacterName = "Rusty"},<br />            new MovieActor() { <br />             FirstName = "Andy", <br />             LastName = "Garcia", <br />             CharacterName = "Terry"},<br />            new MovieActor() { <br />             FirstName = "George", <br />             LastName = "Clooney", <br />             CharacterName = "Dany"},<br />            new MovieActor() { <br />             FirstName = "Jullia", <br />             LastName = "Roberts", <br />             CharacterName = "Tess"}<br />        };<br />    }<br />}<br />
Good news is that this same Distinct() method works for collections of our custom objects too. So if George Clooney accidentally creeps into our collection twice that's not a problem:
var actors = MovieActor.CreateSome();<br />actors.Add(actors[2]);<br /><br />Console.WriteLine(<br /> String.Format("{0} total actors.", actors.Count()));<br /><br />var distinct = actors.Distinct();<br />Console.WriteLine(<br /> String.Format("\n{0} distinct actors.", distinct.Count()));<br /><br />foreach (var actor in distinct)<br />{<br />    Console.WriteLine(actor);<br />}<br />
Not surprisingly, the program prints out our list of actors and despite Mr. Clooney's excellency he is mentioned only once there:
/* Output:<br />5 total actors.<br /><br />4 distinct actors.<br />Brad "Rusty" Pitt<br />Andy "Terry" Garcia<br />George "Dany" Clooney<br />Jullia "Tess" Roberts<br />*/<br />
 
However, it gets a little more difficult in case there are two instances of George:
 
var actors = MovieActor.CreateSome();<br />actors.Add(new MovieActor() { <br /> FirstName = "George", LastName = "Clooney", <br /> CharacterName = "Dany"});<br />
What Distinct() actually does here is comparing object references. So our second George Clooney is in fact an independent object (since it was created independently) and its reference differs from the reference of the first one - therefore it appears in the output twice:
/* Output:<br /><br />5 total actors.<br /><br />5 distinct actors.<br />Brad "Rusty" Pitt<br />Andy "Terry" Garcia<br />George "Dany" Clooney<br />Julia "Tess" Roberts<br />George "Dany" Clooney<br />*/<br />

IEqualityComparer<T>

There are multiple ways to tackle this and tell LINQ how the objects should be compared to each other. I will cover the use of IEqualityComparer<T>. The interface contains two methods:
  • bool Equals(T x, T y) and
  • int GetHashCode(T obj).
The first time I dealt with it I have done a straightforward thing: keeping in mind the fact that I want to compare two objects to check whether they are equal or not, I have implemented Equals(..) part and also introduced very simple GetHashCode(..) version:
 
class ActorComparer : IEqualityComparer<MovieActor><br />{<br />    public bool Equals(MovieActor x, MovieActor y)<br />    {<br />        return<br />            x.LastName == y.LastName &&<br />            x.FirstName == y.FirstName &&<br />            x.CharacterName == y.CharacterName;<br />    }<br /><br />    public int GetHashCode(MovieActor obj)<br />    {<br />        return obj.GetHashCode();<br />    }<br />}<br />
Imagine my surprise when the program spit something like this:
/* Output:<br /><br />5 total actors.<br /><br />5 distinct actors.<br />Brad "Rusty" Pitt<br />Andy "Terry" Garcia<br />George "Dany" Clooney<br />Julia "Tess" Roberts<br />George "Dany" Clooney<br />*/<br />
 
For me it seemed that LINQ simply ignored my equality comparer and kept doing everything on its own. In fact, we could check this by adding a debug output to Equals method:
public bool Equals(MovieActor x, MovieActor y)<br />{<br />    Console.WriteLine(<br />     "Equals called on " + <br />     x.ToString() + " " + <br />     y.ToString());<br />    ...<br />}<br />
And yes, after this the output of the program doesn't change at all. So, what the hell is LINQ doing and why doesn't it even try to use the equality comparer that we have provided? The situation was even bitter for me because I was trying to use Distinct() combined with custom equality comparer to prepare data for pushing to data base, so I ended up with SQL Server telling me that I am breaking a primary key constraint. Because of being focused on a broader problem I didn't take time to think over the stuff that I was trying to use. What I missed was the fact that LINQ, being a query engine, tries to perform as efficiently as possible. More importantly, I have stepped into the trap of thinking that objects, which I use in my code, can be only partially relevant to me, while in case we do OOP every object must be considered in its entirety (things like SOLID help us with that.) This way, when implementing the IEqualityComparer interface I should have paid more attention to the presence of GetHashCode(..) method. This would solve my problem at once, because it is this same method that LINQ uses when asked to extract distinct objects and perform other equality related operations (see Set operators in this article). To verify this we add a debug message to the method and observe several calls to it:
 
public int GetHashCode(MovieActor obj)<br />{<br />    Console.WriteLine(<br />     "Hash called on " + <br />     obj.ToString() + <br />     " (" + obj.GetHashCode() + ")");<br />    return obj.GetHashCode();<br />}<br />
/* Output:<br /><br />5 distinct actors.<br />Hash called on Brad "Rusty" Pitt (46104728)<br />Brad "Rusty" Pitt<br />Hash called on Andy "Terry" Garcia (12289376)<br />Andy "Terry" Garcia<br />Hash called on George "Dany" Clooney (43495525)<br />George "Dany" Clooney<br />Hash called on Julia "Tess" Roberts (55915408)<br />Julia "Tess" Roberts<br />Hash called on George "Dany" Clooney (33476626)<br />George "Dany" Clooney<br />*/<br />
 
Because for reference types default GetHashCode() as well as default Equals(..) is based on references, we still observe both Georges in our printout. Although, now, when we know what exactly goes on under the hood, we can resolve the issue easily. For this purpose let's just use hash of concatenated FirstName, LastName and CharacterName:
public int GetHashCode(MovieActor obj)<br />{<br />    Console.WriteLine(<br />     "\tHash called on " + <br />     obj.ToString() + <br />     " (" + obj.GetHashCode() + ")");<br />    return (obj.FirstName+obj.LastName+obj.CharacterName).GetHashCode();<br />}<br /><br />
While doing this, one may ask why do we still need to implement the Equals(..) method of IEqualityComparer. The output of our refreshed program gives the answer:
5 actors total.<br />        Hash called on Brad "Rusty" Pitt (46104728)<br />Brad "Rusty" Pitt<br />        Hash called on Andy "Terry" Garcia (12289376)<br />Andy "Terry" Garcia<br />        Hash called on George "Dany" Clooney (43495525)<br />George "Dany" Clooney<br />        Hash called on Julia "Tess" Roberts (55915408)<br />Julia "Tess" Roberts<br />        Hash called on George "Dany" Clooney (33476626)<br />        Equals called on George "Dany" Clooney George "Dany" Clooney<br />
So, first, we finally got it working - there is only one George Clooney. Second, notice the last printed line - it shows that our custom Equals method is actually called. With the output that we have it is quite easy to deduce the way Distinct() works:
  1. first, as with all deferred LINQ queries the query object is prepared (that's what the call to Distinct(..) method does)
  2. next, when we iterate over the query object it gets hash code for each item in the source collection and compares it with the hashes of preceding objects
  3. in case a particular hash value was not encountered before, the corresponding object makes its way into the result
  4. if the value was met before, LINQ performs more thorough check for equality calling the Equals(..) method of the comparer for the objects with equal hashes. If this returns true, the second object is forgotten. Otherwise, it gets into the resulting collection.
The last statement basically means that in case we rely on Equals(..) method we could calculate hash based only on the last name and the result will still be the same:
public int GetHashCode(MovieActor obj)<br />{<br />    return obj.LastName.GetHashCode();<br />}<br />
Moreover, if we don't care about efficiency at all, we could make GetHashCode(..) return a constant, thus making Equals(..) entirely responsible for the results we get. Still, I hope that the output will persuade you to avoid this, because the performance impact is significant:
 
public int GetHashCode(MovieActor obj)<br />{<br />    return 0;<br />}<br />
/* Output:<br /><br />5 actors total.<br />        Hash called on Brad "Rusty" Pitt (46104728)<br />Brad "Rusty" Pitt<br />        Hash called on Andy "Terry" Garcia (12289376)<br />        Equals called on Brad "Rusty" Pitt vs Andy "Terry" Garcia<br />Andy "Terry" Garcia<br />        Hash called on George "Dany" Clooney (43495525)<br />        Equals called on Andy "Terry" Garcia vs George "Dany" Clooney<br />        Equals called on Brad "Rusty" Pitt vs George "Dany" Clooney<br />George "Dany" Clooney<br />        Hash called on Julia "Tess" Roberts (55915408)<br />        Equals called on George "Dany" Clooney vs Julia "Tess" Roberts<br />        Equals called on Andy "Terry" Garcia vs Julia "Tess" Roberts<br />        Equals called on Brad "Rusty" Pitt vs Julia "Tess" Roberts<br />Julia "Tess" Roberts<br />        Hash called on George "Dany" Clooney (33476626)<br />        Equals called on Julia "Tess" Roberts vs George "Dany" Clooney<br />        Equals called on George "Dany" Clooney vs George "Dany" Clooney<br />*/<br />
So now we know how to use IEqualityComparer with LINQ and not make a mess of it. I hope this helps. However, I would like to push our ActorComparer a little further. As you might know, LINQ has an OrderBy(..) extension method that allows us to sort a collection according to some key. For this purpose a programmer must provide a function that gets the key from an object - usually this is done via lambda expression. What I would like to do is to provide a way to call the LINQ Distinct(..) method in the same manner. For this purpose we will change the ActorComparer.
 

Generalizing a little

As a result of the modification we must be able to do something like this:
var distinct = actors.Distinct(new ActorComparer(a => a.LastName));<br />
and get a set of actors with unique last names. So we need a way to provide a key selector to our ActorComparer. This is done simply by creating a constructor that takes a function object as an argument and stores it for further use:
public ActorComparer(Func<MovieActor, object> keySelector)<br />{<br />    KeySelector = keySelector;<br />}<br />
The Func<MovieActor, object> is a class standing for something that might be called with MovieActor argument and must yield a result of type object. Although generally I don't like dealing with pure object's in my code, this is a valid way to define a key selector not bounding it to some specific key type. Moreover, in this case this doesn't lead to any problems because we aren't going to use anything except Equals(..) and GetHashCode() methods on the key, so no casts are required. Now we only need to modify Equals(..) and GetHashCode(..) methods (don't confuse these with object.Equals(object o) and object.GetHashCode() ) of our comparer so that they use new KeySelector property:
public bool Equals(MovieActor x, MovieActor y)<br />{<br />    return KeySelector(x).Equals(KeySelector(y));<br />}<br /><br />public int GetHashCode(MovieActor obj)<br />{<br />    return KeySelector(obj).GetHashCode();<br />}<br />
Since our Equals(..) and GetHashCode(..) methods look very similar, a question may arise: why do we need them both? First of all, we already know that we cant get rid of GetHashCode(..) because it is what Distinct(..) uses for comparison in the first place. Okay, let's deal with Equals(..) then: do we still need to compare key values when we have already used hash codes for comparison? Absolutely yes! What makes it inevitable is the idea behind the hash codes.
 
Hash functions that are used to generate hash codes, actually do one thing: they project elements from some data set to a smaller data set (the set of hash codes). The former might be almost anything, while the latter is usually the set of integers. This transformation allows for faster comparison of elements during look-up, because the elements of the second set are easier to compare and because there are fewer of them. Still, due to this same reason any hash function might eventually produce equal codes for non equal objects - this is called hash collision. That's why when LINQ comes across two elements with equal hashes it calls Equals(..) function to check whether the elements are actually equal.
 
This said, let's return to our ActorComparer. You might suggest that to achieve the goal we need to perform some more complex modifications, but no - all we have to do is use the comparer the new way:
var distinct = actors.Distinct(new ActorComparer(a => a.LastName));<br />
The result is the same as when using the first version of ActorComparer, although the new one is much more flexible in the sense that it may be used differently in different contexts and no further modifications are required to its code. Besides, it allows to use more than one property as a key, so the next call is absolutely valid and will preserve all actors with the same last name as long as their first names differ:
var distinct = actors.Distinct(<br /> new ActorComparer(a => <br /> new { a.LastName, a.FirstName }));<br />
The flexibility that this solution offers might be useful when one deals with the movie's sequel. The problem is that Julia Roberts plays two roles there: Tess Ocean and herself:
public static List<MovieActor> CreateSome()<br />{<br />    return new List<MovieActor>()<br />    {<br />        new MovieActor() { <br />         FirstName = "Brad", LastName = "Pitt", <br />         CharacterName = "Rusty"},<br />        new MovieActor() { <br />         FirstName = "Andy", LastName = "Garcia", <br />         CharacterName = "Terry"},<br />        new MovieActor() { <br />         FirstName = "George", LastName = "Clooney", <br />         CharacterName = "Dany"},<br />        new MovieActor() { <br />         FirstName = "Julia", LastName = "Roberts", <br />         CharacterName = "Tess"},<br />        new MovieActor() { <br />         FirstName = "Julia", LastName = "Roberts", <br />         CharacterName = "Julia Roberts"}<br />    };<br />}<br />
Still, with the previous call we'll see her only once in the results. The simple modification of the call to Distinct(..) will solve this issue, while still showing only one copy of George Clooney:
var distinct = actors.Distinct(<br /> new ActorComparer(<br />  a => <br />  new { a.LastName, a.FirstName, a.CharacterName }));<br />
/* Output:<br /><br />6 actors total.<br />Brad "Rusty" Pitt<br />Andy "Terry" Garcia<br />George "Dany" Clooney<br />Julia "Tess" Roberts<br />Julia "Julia Roberts" Roberts<br />*/<br />

Conclusion

This is it. We have explored the interaction between LINQ extension methods and custom IEqualityComparers and even implemented one. The resulting class is both easy to use and highly customizable, because its operation is fully defined by the key selector function provided by user. Furthermore, it is very easy to make the class generic so that it can be used for collections of objects of other types - not only for MovieActors. The complete code for this example is available through github. (There is also a generic version of our comparer.)
 
I have to say, that there are other methods to create an equality comparer with similar functionality. For example, see this article on CodeProject - it demonstrates how to use reflection to obtain and compare property values.
 
Finally, if you just need to filter collection for distinct values based on some key and you want to do it quickly with as few additional actions as possible, there is a trick that doesn't require creating new types:
return actors.GroupBy(a => new { <br />    a.LastName, a.FirstName, a.CharacterName }).<br />    Select(g => g.First());<br />
Note that IEqualityComparer may (and should) be used to perform more complex comparisons, however its implementation won't get much more complex in most cases.
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Acumatica
Russian Federation Russian Federation
I am a full-time .NET programmer and a lover of C#, C++, Clojure and Python at the same time. I do some Windows Phone development on my own and occasionally try myself at web-development. To enhance all these software-related activities I maintain a blog writing there on various topics, most of which actually come back to programming.

Comments and Discussions

 
QuestionProblem of <br/> tag all over the places Pin
Mystcreater10-May-17 7:04
Mystcreater10-May-17 7:04 
Hi,

In the samples of code, there are
instead of interpreting the carriage return.

The code is totally not readable because of that.
GeneralNice and deep explanation Pin
Mystcreater3-Oct-14 9:24
Mystcreater3-Oct-14 9:24 
GeneralRe: Nice and deep explanation Pin
Alex Turok6-Oct-14 4:55
Alex Turok6-Oct-14 4:55 
Generalthanks for the clarification Pin
taleofsixstrings19-Aug-14 3:39
taleofsixstrings19-Aug-14 3:39 
GeneralRe: thanks for the clarification Pin
Alex Turok19-Aug-14 4:15
Alex Turok19-Aug-14 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.