加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将谓词作为参数传递给Where子句时,EF SQL发生了变化

发布时间:2020-12-15 07:42:39 所属栏目:百科 来源:网络整理
导读:EF为下面列出的两个类似语句生成不同的SQL var test = dbcontext.Persons.GetAll() .Where(c = c.PersonID == 2) .Select(c = c.PersonName) .FirstOrDefault();` 生成的SQL: SELECT [Limit1].[PersonName ] AS [PersonName ]FROM (SELECT TOP (1) [Extent1
EF为下面列出的两个类似语句生成不同的SQL
var test = dbcontext.Persons.GetAll()
                            .Where(c => c.PersonID == 2)
                            .Select(c => c.PersonName)
                            .FirstOrDefault();`

生成的SQL:

SELECT 
    [Limit1].[PersonName ] AS [PersonName ]
FROM 
    (SELECT TOP (1)
         [Extent1].[PersonName ] AS [PersonName ]
     FROM 
         [dbo].[ApplicationRequest] AS [Extent1]
     WHERE 
         [Extent1].[PersonID ] = @p__linq__0) AS [Limit1]',N'@p__linq__0 uniqueidentifier',@p__linq__0= "2"

我在具有不同Where条件的多个地方使用上述陈述;在一个地方合并逻辑我将条件作为参数传递

Public Void PassPredicate(Func<ApplicationRequest,bool> ReqFunc)
{
    var test = dbcontext.Persons.GetAll()
                                .Where(ReqFunc)
                                .Select(c => c.PersonName)
                                .FirstOrDefault();
}

我把这个函数称为

PassPredicate(c => c.PersonID == 2);

生成的SQL:

SELECT 
    [Extent1].[PersonID] AS [PersonID],[Extent1].[PersonName ] AS [PersonName ],[Extent1].[DOB] AS [Dob],[Extent1].[Height] AS [Height],[Extent1].[BirthCity] AS [BirthCity],[Extent1].[Country] AS [Country],FROM 
    [dbo].[Person] AS [Extent1]

如果你看第二个SQL,它是非常惊人的:它是拉所有信息(列和行).它没有where子句并选择所有列.

从DB返回结果后应用where条件.

第二个语句的唯一区别是我将条件作为参数传递,而不是在where子句中有条件.

谁能解释为什么会有区别?

解决方法

由于ReqFunc类型是Func< ApplicationRequest,bool>您正在使用Enumerable扩展,因此您的代码(Where,Select,FirstOrDefault)将在内存中执行.

要解决此问题,只需将ReqFunc更改为Expression< Func< ApplicationRequest,bool>>使用可查询扩展:

Public Void PassPredicate(Expression<Func<ApplicationRequest,bool>> ReqFunc)
{
    var test = dbcontext.Persons.GetAll().Where(ReqFunc).Select(c => c.PersonName).FirstOrDefault();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读