我可以在实体框架的方法在哪里使用自定义的委托方法? [英] Can I use custom delegate method in Where method of Entity Framework?

查看:88
本文介绍了我可以在实体框架的方法在哪里使用自定义的委托方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);

这篇关于我可以在实体框架的方法在哪里使用自定义的委托方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆