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());

25 April, 2010

First post

Hello Blog World.

This is my first blog and consequently my first blog post.

Never been much of a writer and never had a reason to start a blog but lately I've found that I couldn't easily find answers to some of the questions i had. After some time I ended up with what i needed but if I had just found it earlier i could have met the deadline. So this blog is just a repository for those answers so that you may be on time.

This will be a software development blog so if you're not a developer this may not be your cup of tea.

I will focus on .NET, C# , Silverlight, WCF (my specialties so far) and hopefully in the near future Mobile platforms (iPhone, Android and Windows Phone 7).

This first blog post was just an overview of the subjects I will be writing about.
From now on just technical stuff.

See ya later.