简体   繁体   中英

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. The LINQ to SQL provider for example produces the exact same SQL for both queries:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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