dotnet / dotnet/efcore

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

Open
#34,958 5 comments 0 reactions 0 assignees View on GitHub
area-perf area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

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

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

Expected:

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

Actual:

```sql
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.