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]
Distinct[MyDOClass](System.Linq.IQueryable
1[DAL.MyDOClass],
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…