Linq distinct with lambda
Linq Distinct function accepts an IEqualityComparer<T> used to compare values of the IEnumerable to remove equal objects to implement the Distinct() function. Sometimes it is more useful to specify a simple Func<T, T, Boolean>, a simple predicate that will implement the concept of Equality between elements. To support such a scenario you can write this simple Extension method.
All the work is done by the DistinctByLambda class, that accepts a reference to the original IEnumerable sequence of elements and the predicate to verify for equality. Here is the code.
The code is really simple, the IEnumerable.GetEnumerator function is implemented with a simple internal list that stores all distinct elements. For each element of the original list, if it is not equal to any element of the original list it is stored in the list and then returned to the caller; if the element is equal to one element in the list, it gets not returned.
You can use the following function in this simple way.
I use a simple Test class that contains two properties, and call Distinct with a function that compare only the BlaBla property. Now you can use lambda with distinct