c# - Is there a way to inline external functions into an EF Linq query? -


After

let's say I have a function like this:

  on filterValue = GetCurrentFilter ( state) ;  

And then an EF query:

  var result = context. AntitySet.Where (x => x.column & gt; filterValue);  

This works, but as soon as I try to inline it:

  var result = context. Antisetset Where (x = & gt; x.column & gt; GetCurrentFilter (state));  

It is not because EF link tried to parse GetCurrentFilter in the expression tree and it is unable to do it all this can be quite understood is.

My question is, there EF Linq knows that in order to go for execution of GetCurrentFilter in a way when it creates trees and use its results In the tree

like the result on
  = context.EntitySet.Where (x => x.column & gt; EfUtil.ResultOf (GetCurrentFilter (state)) );  

Since GetCurrentFilter does not have parameters that are a part of the query, it is technically possible that if EF Linux can support it. I suspect that I am losing the right syntax for this.

Make GetCurrentFilter instead of one method (read only) property < / P>


You have the only other way to cross the entire expression forest, search for your use, EF evaluate their values ​​instead of trying to translate their properties into SQL Will do ResultOf method, evaluating your parameters for a value, and then that value was inline where ResultOf was once called, rebuiding the query around that value.

To work for this, it means that you do not need to wrap the code only, which you want to call in the call on EfUtil.ResultOf , but this means It is also stressing on the query to call a method to go back and evaluate it:

  public class EfUtil {public static t result>  (t-value) {returns value; }} // Note This is probably a better name public stable IQueryable & lt; T & gt; EvaluateResults & LT; T & gt; T & gt; (The IQueryable & lt; t & gt; query) {query.Provider.CreateQuery & lt Return (. New ExpressionEvaluator ()) (query.Expression); } Internal class ExpressionEvaluator: ExpressionVisitor {Protected override expression VisitMethodCall (MethodCallExpression m) {if (m.Method.Name == "ResultOf" & amp; m.Method.DeclaringType == typeof (EfUtil)) {Expression Target = Me Logic [0]; Object result = expression. Lambda (goal). Compile (). Dynamic invoc (); Return expression. Constant (result, target type); } And return basis. Visit Model Clall (m); }}  

This will allow you to write:

  var result = context. AntitySet.Where (x => x.column & gt; EfUtil ResultOf (GetCurrentFilter (state)). Avail Results ();  

This is then the GetCurrentFilter (State) and mark the result as continuous in the query.

A little simple test, we can write the following:

  var Query = new [] {1, 2, 3.}. Enqueueable (). (X => gt; x & gt; EfUtil.ResultOf (Math.max (1, 2)) Evaluate Results (); Console.WriteLine (Query.ToString ());  

and it will print:

System.Int32 []. Yes (x => (x> 2))

We want what we want.

Note that the parameters of Lambda (in these instances X ). FUTL result / or code will not work anywhere within the call, and probably can not be done to work (although we can take enough care Can generate a better error message).


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -