木子屋 Dnawo's BLOG

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

👤 dnawo 📅 2014-12-25 👁 3026 👍 0 💬 0 🔄 来源:本站原创
不使用表达式树:

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)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 表达式树ExpressionTree[转] 下一篇 → 同一页面jQuery多个版本或和其他js库冲突解决方法