Good Morning!
Given:
public class FooClass
{
public void FooMethod()
{
using (var myEntity = new MyEntity)
{
var result = myEntity.MyDomainEntity.Where(myDomainEntity => myDomainEntity.MySpecialID > default(int)).Distinct(new FooComparer);
}
}
}
public class FooComparer : IEqualityComparer<MyEntity.MyDomainEntity>
{
public bool Equals(MyEntity.MyDomainEntity x, MyEntity.MyDomainEntity y)
{
return x.MySpecialID == y.MySpecialID;
}
public int GetHashCode(MyEntity.MyDomainEntity obj)
{
return obj.MySpecialID.GetHashCode();
}
}
This will compile, but on runtime I will get an Linq to Entity could not translate Comparer-Exception.
Any suggestions?
If you’re providing your own comparisons, you’ll need to execute the
Distinctcall in .NET code. To make sure that happens, useAsEnumerableto turnIQueryable<T>intoIEnumerable<T>:Of course at that point you’ll be pulling more data across from the database. An alternative is to group the data instead:
That will get you the first entity encountered with each ID (assuming my query-fu isn’t failing me). That’s basically what Distinct does anyway, but it’s all at the database.
(Note to future readers: calling
First()makes more sense thanFirstOrDefault(), but apparently that doesn’t work.)