So in things like GroupBy()
in Linq, you can provide an implementation of IEqualityComparer<T>
to help with object comparisons. It seems, though, that it would be easier to simply pass in a lambda expression.
Example:
// current implementation
myCollection.GroupBy(c => c.Foo, c => c.Bar, new FooBarComparer());
// it seems easier to...
myCollection.GroupBy(c => c.Foo, c => c.Bar, (x, y) => x.Baz == y.Baz);
Given a simple implementation of IEqualityComparer<T>
like this:
public class FooBarComparer : IEqualityComparer<FooBar> {
public bool Equals(FooBar x, FooBar y) {
return x.Baz == y.Baz;
}
public int GetHashCode(FooBar obj) {
return obj.GetHashCode();
}
}
It seems that providing a lambda expression could be just as effective. As it stands now, if I try to pass in an IEqualityComparer<T>
with a Linq query to a database, it fails because SQL Server (or whatever) doesn't know anything about my class. It seems that a lambda would be able to be translated into SQL that can be used in the target database.
Is there a specific reason this is not provided as an option in Linq?