Linq-to-Entities / Linq-to-SQL, is one data polling method more efficient?

Are the following two examples different, and if so, 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); 
+4
source share
3 answers

They are functionally equivalent. There may be slight differences in how they are implemented in various LINQ providers, but I would expect almost flat performance. For example, the LINQ to SQL provider creates exactly the same SQL for both queries:

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

I expect the same to apply to the Entity Framework.

If I were to choose one, I would choose the second version, because it is more concise, without loss of clarity. In fact, I would say that this is more understandable - there is less noise for keywords, so the business logic stands out more.

+4
source

They will both be identical (SQL and performance).

+2
source

They are the same. The compiler should give the same result for LINQ syntax.

+1
source

All Articles