While working on a side project for a friend who needed a program to compare two files, I discovered a handy extension method called ‘Except’.  This method allows you to take two lists and find the set difference between the two.  For strings, this was pretty straight-forward and I just followed the example from MSDN.

Example

var differenceQuery = sourceList.Except(externalList);

The result of this query will be an IEnumerable of the type from each list (in this case it was a string).  The list will contain everything from “sourceList” that does not exist in “externalList”.

What do you do if you need to check lists of your own classes?

Just implement an IEqualityComparer<T> for your class.  We’ll cover that in a future post!

image