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

raj*_*cle 7 .net c# linq entity-framework

EF为下面列出的两个类似语句生成不同的SQL

var test = dbcontext.Persons.GetAll()
                            .Where(c => c.PersonID == 2)
                            .Select(c => c.PersonName)
                            .FirstOrDefault();`
Run Code Online (Sandbox Code Playgroud)

生成的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"
Run Code Online (Sandbox Code Playgroud)

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

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

我把这个函数称为

PassPredicate(c => c.PersonID == 2);
Run Code Online (Sandbox Code Playgroud)

生成的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]
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Art*_*aca 6

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

为了解决这个问题,只需更改ReqFuncExpression<Func<ApplicationRequest, bool>>使用Queryable扩展:

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