Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top