10 July, 2010

Comparing collections with LINQ

Once in a while i have to call the Distinct LINQ method and, more often than not, i don't want to compare the objects reference. To this effect Microsoft provides the interface IEqualityComparer<T>.The IEqualityComparer<T> interface implements two methods: bool Equals(T x, T y) and int GetHashCode(T obj). These methods are both used by LINQ to compare objects in collections.Since i need to compare most of the business objects in my solutions, and don't want to go through the hassle of implementing tens of new classes (one per object) I wanted a generic solution.Simple enough, I built myself the GenericComparer<T>.public class GenericComparer<T> : IEqualityComparer<T> { public GenericComparer(Func<T, T, bool> equals, Func<T, int> getHashCode) { this.equals = equals; this.getHashCode = getHashCode; } readonly Func<T, T, bool> equals; public bool Equals(T x, T y) { return equals(x, y); } readonly Func<T, int> getHashCode; public int GetHashCode(T obj) { return getHashCode(obj); } }

This worked fine. I could finally just set my comparer directly in my LINQ command. All i had to give was the expression to be used in the Equals and GetHashCode methods.

IEnumerable<mytype> result = collection.Distinct(new GenericComparer<mytype>(((mt1, mt2) => mt1.id == mt2.id), (mt => mt.id.GetHashCode()))); Still, it was more code than I would like to see. Since all I want is to receive all unique elements based on a single expression then that's all i should have to write. A simple tweak to the GenericComparer class and I got a simpler comparer SimpleGenericComparer<T>.public class SimpleGenericComparer<T> : IEqualityComparer<T> { public SimpleGenericComparer(Func<T, int> getHashCode) { this.getHashCode = getHashCode; } public bool Equals(T x, T y) { return getHashCode(x) == getHashCode(y); } readonly Func<T, int> getHashCode; public int GetHashCode(T obj) { return getHashCode(obj); } } Having the Equals method simply compare both objects with the GetHashCode expression i could finaly just give the expression with which the comparison will be made.IEnumerable<mytype> result = collection.Distinct(new SimpleGenericComparer<mytype>(mt => mt.id.GetHashCode());