简体   繁体   English

Linq-to-Entities / Linq-to-SQL,一种数据轮询方法更有效吗?

[英]Linq-to-Entities/Linq-to-SQL, Is One Data Polling Method More Efficient?

Are the following two examples different and if so, which is more efficient? 以下两个示例是否不同?如果不同,哪个更有效?

var user = (from u in db.Users where u.Id == userId select u).Single();

and

var user = db.Users.Single(p => p.Id == userId);

They are functionally equivalent. 它们在功能上是等效的。 There could be slight differences in how they are implemented in the various LINQ providers, but I would expect almost exactly equal performance. 在各种LINQ提供程序中实现它们的方式可能略有不同,但是我希望性能几乎完全相同。 The LINQ to SQL provider for example produces the exact same SQL for both queries: 例如,LINQ to SQL提供程序会为两个查询生成完全相同的SQL:

SELECT [t0].[Id], [t0].[Name]
FROM [dbo].[User] AS [t0]
WHERE [t0].[Id] = @p0

I expect that the same applies to the Entity Framework. 我希望这同样适用于实体框架。

If I had to pick one I'd choose the second version because it is more concise with no loss of clarity. 如果我必须选择一个版本,我会选择第二个版本,因为它更简洁,而且不失清晰。 In fact I would say it is more clear - there is less keyword noise so the business logic stands out more. 实际上,我想说的很清楚-关键字干扰少,因此业务逻辑更加突出。

它们最终将是完全相同的(SQL和效率明智)。

They are the same. 他们是一样的。 The compiler should produce the same outcome for both LINQ syntax. 两种LINQ语法的编译器应产生相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM