我可以在实体框架的方法在哪里使用自定义的委托方法?方法、自定义、实体、框架

2023-09-04 23:18:55 作者:画个圈圈诅咒你

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

我传递参数,如果方法为: F =&GT; f.Id&GT; 4 。 我可以通过 f.Id&GT的委托方法来代替; 4

推荐答案

没有。

实体框架必须能够为看的一切,正在尝试。

The Entity Framework needs to be able to see everything that is being attempted.

所以,如果你只是做了这样的事情:

So if you simply did something like this:

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

在哪里DelegateFunc的定义是这样的:

Where the definition of DelegateFunc looks like this:

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

实体框架已经没有办法对等体的代表在里面,破解它打开并将其转换为SQL的。

The Entity Framework has no way of peering inside the delegate, to crack it open and convert it to SQL.

所有未虽然输了。

如果你的目标是重新使用常用的过滤器等等,你可以做这样的事情,而不是:

If your goal is to re-use common filters etc you can do something like this instead:

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

queryable.Where(DelegateExpression);
 
精彩推荐
图片推荐