Reputation: 5994
First of all: I know that there were already many questions before about this topic. But I really could not find any solutions mor MY problem. My problem is that I am using an abstract method to select from an dbset. My code looks like this:
var dbe = (from i in dbEntities where IsEqualRecord(me, i) select i);
And this is my abstract method deklaration:
protected abstract bool IsEqualRecord(MEntity modelEntities, DEntity databaseEntity);
MEntity
and DEntity
are generic-types. I've read that my where Statement can't be translated to an sql statement. But how can I solve that problem? Is there any other approach?
Please don't vote to close this question. I've took a look at nearly every similar question on stackoverflow but I could not find a solution.
Upvotes: 4
Views: 498
Reputation: 292425
Linq to Entities queries are translated to SQL to be executed by the DBMS, so you can only use code that can be translated to SQL. A custom C# method cannot be translated to SQL, so EF fails to translate the query. The fact that it's abstract is irrelevant.
A possible workaround would be to replace IsEqualRecord
by a method that returns an Expression<Func<DEntity, bool>>
that can be translated to SQL; you could then do something like that:
var dbe = dbEntities.Where(MakeEqualityPredicate(me));
...
protected abstract Expression<Func<DEntity, bool>> MakeEqualityPredicate(MEntity m);
The MakeEqualityPredicate
method should return an Expression that compares a DEntity
to the given MEntity
. For instance, a derived class could implement it like this:
protected override Expression<Func<DEntity, bool>> MakeEqualityPredicate(MEntity m)
{
return d => d.Id == m.Id;
}
Upvotes: 5