문제

I am using LinqKit and I want to write a predicate in which the code has to call a plain boolean method, like this :

var predicate = PredicateBuilder.False<MyEntity>();

var predicate = predicate.And(myEntity => this.EntityMatches(myEntity));

this.ObjectSet.AsExpandable().Where(predicate).ToList();

Here is (part of) the EntityMatches method :

private bool EntityMatches(MyEntity myEntity)
{
    bool isMatch;

    // I make a call to another library boolean method here.
    isMatch = otherLib.IsEntityMatch(myEntity);

    // I perform some other checks right after.
    isMatch &= // some other conditions ...

    return isMatch;
}

I get this exception when running deferred execution :

LINQ to Entities does not recognize the method 'Boolean EntityMatches(MyEntity)' method, and this method cannot be translated into a store expression.

How can I rewrite the EntityMatches method so it is understandable by the store provider ?

도움이 되었습니까?

해결책

LinqKit is not magic, it can't see inside EntityMatches() or otherLib.IsEntityMatch(), so there is no way it can help you translated those into SQL.

What LinqKit can do is to create a complicated condition out of simple parts. But each simple part has to be translatable into SQl by itself and I don't see a way how to do that in your case, especially since you're using some external library.

다른 팁

You should convert your method to Linq Expression. Please read this maybe it will help IQueryable extension method for linq2entities

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top