Make the method GetCurrentFilter a (read-only) instead of the method. EF will evaluate properties by their values, and not try to translate them into SQL, unlike methods.
The only other way that you have is to go through the entire expression tree, look for the use of your ResultOf method, evaluate its parameter to a value, and then embed that value when the ResultOf call was once, retraining the query around that value .
For this to work, this means that you not only need to wrap the code you want to embed in the EfUtil.ResultOf call, but also means calling the method in the request itself to make it return and evaluate this:
public class EfUtil { public static T ResultOf<T>(T value) { return value; } }
This will allow you to write:
var result = context.EntitySet.Where(x=> x.column > EfUtil.ResultOf(GetCurrentFilter(state))) .EvaluateResults();
He would then evaluate GetCurrentFilter(state) on the client side and inject the result as a constant into the request.
As a slightly simpler test, we can write the following:
var query = new[] { 1, 2, 3 } .AsQueryable() .Where(x => x > EfUtil.ResultOf(Math.Max(1, 2))) .EvaluateResults(); Console.WriteLine(query.ToString());
And he will print:
System.Int32 []. Where (x => (x> 2))
This is exactly what we want.
Note that using the lambda parameter ( x in these examples) cannot be used anywhere in the call to EfUtil.ResultOf , or the code will not work and cannot be executed to work (although we can generate a better error message if we care enough )
Servy
source share