Can I use custom delegate method in Where method of Entity Framework?(我可以在实体框架的 Where 方法中使用自定义委托方法吗?)
问题描述
Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate);
我将参数传递给 Where 方法如下: f =>f.Id >4代码>.我可以通过委托方法而不是
f.Id >4
?
I pass parameter to Where method as follows: f => f.Id > 4
.
Can I pass a delegate method instead of f.Id > 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);
这篇关于我可以在实体框架的 Where 方法中使用自定义委托方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以在实体框架的 Where 方法中使用自定义委托方法吗?


- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01