Count(x => ...) vs Where(x => ...).Count()

本文关键字:gt Count Where vs | 更新日期: 2023-09-27 18:01:44

这两个LINQ-to-Entities查询有什么区别吗?

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

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

的性能和生成的SQL?

我试着自己看生成的SQL,但我只知道如何从IQueryable获得SQL,但Count直接返回值。

Count(x => ...) vs Where(x => ...).Count()

我已经设法看到SQL(感谢@dasblinkenlight),答案是-不,两个LINQ查询生成完全相同的SQL查询,至少对于没有分组的简单查询:

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[Table] AS [Extent1]
        WHERE <condition>
    )  AS [GroupBy1]