Can I use a custom delegation method in the Where method for Entity Framework?

Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate);

I pass parameters Where, as follows: f => f.Id > 4. Can I pass a delegate method instead f.Id > 4?

+3
source share
1 answer

No.

In the Entity Framework, it should be possible to see everything that is being done.

So, if you just did something like this:

queryable.Where(f => DelegateFunc(f));

If the definition of DelegateFunc is as follows:

public bool DelegateFunc(Foo foo)
{
   return foo.Id > 4;
}

Entity Framework has no way to look inside the delegate, hack it and convert it to SQL.

All is not lost.

.., - :

public Expression<Func<Foo, bool>> DelegateExpression{
   get{
       Expression<Func<Foo,bool>> expr = f => f.Id > 4;
       return expr;
   }
}

queryable.Where(DelegateExpression);
+11

Source: https://habr.com/ru/post/1766317/


All Articles