0

Is there any difference between these two LINQ-to-Entities queries:

context.Table.Count(x => ...)

and

context.Table.Where(x => ...).Count()

in terms of performance and generated SQL?

I tried to look generated SQL myself, but I only know how to get the SQL from IQueryable, but Count returns the value directly.

Aleksey Shubin
  • 1,960
  • 2
  • 20
  • 39

1 Answers1

0

I have managed to see the SQL (thanks to @dasblinkenlight), the answer is - no, both LINQ queries generate exactly the same SQL query, at least for a simple query without a grouping:

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[Table] AS [Extent1]
        WHERE <condition>
    )  AS [GroupBy1]
Aleksey Shubin
  • 1,960
  • 2
  • 20
  • 39
  • there may be a little difference since in the second solution there is second function call (first the call to the where function and then a Count(). I don't think this makes sooo much difference when use rarely – Radinator Aug 10 '16 at 09:27
  • Yes, but I think the additional function call time is negligible compared to overall database connection and SQL execution time. – Aleksey Shubin Aug 10 '16 at 09:31
  • as i said in my post :D – Radinator Aug 10 '16 at 10:44