使用动态编译谓词时,EF 不会生成正确的 SQL SELECT WHERE
我正在尝试创建动态编译谓词并将其用于查询实体框架对象集。 执行以下代码并返回正确的结果,但生成的 SQL SELECT 语句没有 WHERE 子句:
var db = new DBHelper();
ParameterExpression entity = Expression.Parameter(typeof(HighSchoolServicesDataAccess.Faculty), "entity");
var filterentity = Expression.Lambda(Expression.Equal(Expression.Property(entity, "HighSchoolID"), Expression.Constant(90, typeof(int))), entity);
Func<HighSchoolServicesDataAccess.Faculty, bool> predicate = (Func<HighSchoolServicesDataAccess.Faculty, bool>)filterentity.Compile();
var res = db.DBContext.Faculties.Where(predicate);
dataGridView2.DataSource = res.ToList();
生成的 SQL 语句是:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[HighSchoolID] AS [HighSchoolID],
[Extent1].[TypeID] AS [TypeID],
[Extent1].[Name] AS [Name]
FROM [dbo].[Faculties] AS [Extent1]
当我使用显式表达式(如
var res = db.DBContext.Faculties.Where(f => f.HighSchoolID == 90);
正确的 SQL)时,会生成。
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[HighSchoolID] AS [HighSchoolID],
[Extent1].[TypeID] AS [TypeID],
[Extent1].[Name] AS [Name]
FROM [dbo].[Faculties] AS [Extent1]
WHERE 90 = [Extent1].[HighSchoolID]
我如何强制 EF 在 SQL 中生成 WHERE 子句?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定,但可能是您将
predicate
设置为已编译的Func<...>
而不是将其保留为Expression ...>>
,所以在 EF 有机会处理它之前它就被转换为 IL 了?你尝试过吗?
Not sure, but could it be that you're setting
predicate
to a compiledFunc<...>
and not keeping it as anExpression<Func<...>>
, so it's getting converted to IL before EF has a chance to process it? Have you tried e.g.?
您创建了谓词,但最后编译了它,这意味着它不再是表达式,一旦您将其传递到您使用 linq-to-objects 而不是 linq-to-entities 的位置。这意味着所有记录都会传输到应用程序,并且过滤是在内存中完成的。
试试这个:
You created predicate but at the end you compiled it which means you it is not expression any more and once you pass it to the Where you are using linq-to-objects instead of linq-to-entities. It means that all records are transferred to the application and filtering is done in memory.
Try this instead:
Linq 需要函数的表达式,而不是编译后的函数。请使用 Reflector 查看编译器为您的 linq 表达式生成的内容。
整个表达式树应该可用于 Linq where 子句而无需编译。
Linq needs Expression of function, not a compiled function. Please use reflector to see what is generated by compiler for your linq expression.
Enire expression tree should be available to Linq where clause without compilation.