Skip to content

DbSet.Count() in Select() lambda not translating to scalar subquery expression #34958

@Bretttt

Description

@Bretttt

It appears a DbSet.Count() expression is getting evaluated ahead of time and parameterized.

Example:

var query = dbContext.MyDbSet.Select(v => new
{
    v.Id,
   Count = dbContext.MyDbSet.Count()
});
string sql = query.ToQueryString();

Expected:

SELECT [v].[Id], (select COUNT(1) from [MyTable]) AS [Count]
FROM [MyTable] AS [v]

Actual:

DECLARE @__Count_0 int = 20;

SELECT [v].[Id], @__Count_0 AS [Count]
FROM [MyTable] AS [v]

Maybe this is by design for some reason I'm not understanding. Thank you

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions