LINQ to Entities使用表达式树生成的SQL语句比较

不使用表达式树:

var query = context.People.Where(item => item.Age == 20);

引用内容 引用内容
Select
    [Extent1].[Id] AS [Id],
    [Extent1].[Name] AS [Name],
    [Extent1].[Age] AS [Age]
    FROM [dbo].[Person] AS [Extent1]
    Where 20 = [Extent1].[Age]

使用表达式树:

Expression<Func<Person, bool>> exp = item => item.Age == 20;
var query = context.People.Where(exp.Compile());

引用内容 引用内容
Select
    [Extent1].[Id] AS [Id],
    [Extent1].[Name] AS [Name],
    [Extent1].[Age] AS [Age]
    FROM [dbo].[Person] AS [Extent1]

结论:使用表达式树生成的SQL语句不包含查询条件,这样返回的记录比不使用表达式树时多。

评论: 0 | 引用: 0 | 查看次数: 2192
发表评论
登录后再发表评论!