I have a L2E query that returns some data that contains duplicate objects. I need to remove those duplicate objects. Basically I should assume that if their IDs are the same then the objects are duplicate. I’ve tried q.Distinct(), but that still returned duplicate objects. Then I’ve tried implementing my own IEqualityComparer and passing it to the Distinct() method. The method failed with following text:
LINQ to Entities does not recognize the method
‘System.Linq.IQueryable1[DAL.MyDOClass]1[DAL.MyDOClass],
Distinct[MyDOClass](System.Linq.IQueryable
System.Collections.Generic.IEqualityComparer`1[DAL.MyDOClass])’
method, and this method cannot be translated into a store expression.
And here is the implementation of EqualityComparer:
internal class MyDOClassComparer: EqualityComparer<MyDOClass>
{
public override bool Equals(MyDOClass x, MyDOClass y)
{
return x.Id == y.Id;
}
public override int GetHashCode(MyDOClass obj)
{
return obj == null ? 0 : obj.Id;
}
}
So how do I write my own IEqualityComparer properly?
An
EqualityCompareris not the way to go – it can only filter your result set in memory eg:You can use the
GroupBymethod to group by IDs and theFirstmethod to let your database only retrieve a unique entry per ID eg: