使用动态编译谓词时,EF 不会生成正确的 SQL SELECT WHERE

发布于 2024-11-04 07:37:25 字数 1298 浏览 0 评论 0 原文

我正在尝试创建动态编译谓词并将其用于查询实体框架对象集。 执行以下代码并返回正确的结果,但生成的 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 子句?

I am trying to create dynamic compiled predicate and use it for querying Entity Framework ObjectSet.
Following code is executed and correct result is returned,but SQL SELECT Statement is generated without WHERE clause:

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

Generated SQL statement is:

SELECT 
    [Extent1].[ID] AS [ID], 
    [Extent1].[HighSchoolID] AS [HighSchoolID], 
    [Extent1].[TypeID] AS [TypeID], 
    [Extent1].[Name] AS [Name]
FROM [dbo].[Faculties] AS [Extent1]

and when I use explicit expression like

var res = db.DBContext.Faculties.Where(f => f.HighSchoolID == 90);

correct SQL is generated.

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]

How can i force EF to produce WHERE clause in SQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-11-11 07:37:25

不确定,但可能是您将 predicate 设置为已编译的 Func<...> 而不是将其保留为 Expression ...>>,所以在 EF 有机会处理它之前它就被转换为 IL 了?你尝试过吗

var predicate = 
   (Expression<Func<HighSchoolServicesDataAccess.Faculty, bool>>)filterentity;

Not sure, but could it be that you're setting predicate to a compiled Func<...> and not keeping it as an Expression<Func<...>>, so it's getting converted to IL before EF has a chance to process it? Have you tried e.g.

var predicate = 
   (Expression<Func<HighSchoolServicesDataAccess.Faculty, bool>>)filterentity;

?

丢了幸福的猪 2024-11-11 07:37:25

您创建了谓词,但最后编译了它,这意味着它不再是表达式,一旦您将其传递到您使用 linq-to-objects 而不是 linq-to-entities 的位置。这意味着所有记录都会传输到应用程序,并且过滤是在内存中完成的。

试试这个:

var res=db.DBContext.Faculties.Where(filterentity);
dataGridView2.DataSource = res.ToList();

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:

var res=db.DBContext.Faculties.Where(filterentity);
dataGridView2.DataSource = res.ToList();
七色彩虹 2024-11-11 07:37:25

Linq 需要函数的表达式,而不是编译后的函数。请使用 Reflector 查看编译器为您的 linq 表达式生成的内容。

整个表达式树应该可用于 Linq where 子句而无需编译。

 var db = new DBHelper();

 ParameterExpression entity = 
    Expression.Parameter(typeof(HighSchoolServicesDataAccess.Faculty), 
        "entity");

 Expression<Func<HighSchoolServicesDataAccess.Faculty, bool>> filterentity = 
    Expression.Lambda<Func<HighSchoolServicesDataAccess.Faculty, bool>>(
    Expression.Equal(
      Expression.Property(entity, "HighSchoolID"), 
        Expression.Constant(90, typeof(int))), entity);

 // not at all needed...
 //Func<HighSchoolServicesDataAccess.Faculty, bool> predicate =
 //   (Func<HighSchoolServicesDataAccess.Faculty, bool>)filterentity.Compile();

 var res = db.DBContext.Faculties.Where(filterentity);
 dataGridView2.DataSource = res.ToList();

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.

 var db = new DBHelper();

 ParameterExpression entity = 
    Expression.Parameter(typeof(HighSchoolServicesDataAccess.Faculty), 
        "entity");

 Expression<Func<HighSchoolServicesDataAccess.Faculty, bool>> filterentity = 
    Expression.Lambda<Func<HighSchoolServicesDataAccess.Faculty, bool>>(
    Expression.Equal(
      Expression.Property(entity, "HighSchoolID"), 
        Expression.Constant(90, typeof(int))), entity);

 // not at all needed...
 //Func<HighSchoolServicesDataAccess.Faculty, bool> predicate =
 //   (Func<HighSchoolServicesDataAccess.Faculty, bool>)filterentity.Compile();

 var res = db.DBContext.Faculties.Where(filterentity);
 dataGridView2.DataSource = res.ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文