I have this piece of C# code (using EF Core 3.1) that performs a SQL left join operation.
var groupedQuery = query.GroupBy(x => x.CreatedTime.Date)
.Select(o => new
{
Date = o.Key,
Count = o.Count()
});
var acceptedGroupedQuery = query.Where(o => o.Accepted).GroupBy(x => x.CreatedTime.Date)
.Select(o => new
{
Date = o.Key,
Count = o.Count()
});
var projectedQuery = groupedQuery.GroupJoin(acceptedGroupedQuery, o => o.Date, i => i.Date,
(o, i) => new { Total = o, Accepted = i })
.SelectMany(o => o.Accepted.DefaultIfEmpty(), (joined, accepted) => new
{
Date = joined.Total.Date,
Total = joined.Total.Count,
Accepted = accepted.Count
});
var result = await projectedQuery
.Skip(model.Skip).Take(model.Take).ToArrayAsync(cancellationToken: cancellationToken);
The corresponding SQL generation is
exec sp_executesql N'SELECT [t].[c] AS [Date], CAST([t].[c0] AS float) AS [Total], CAST([t0].[c0] AS float) AS [Accepted]
FROM (
SELECT CONVERT(date, [p].[CreatedTime]) AS [c], COUNT(*) AS [c0]
FROM [PartnerInvitation] AS [p]
WHERE [p].[DeletedTime] IS NULL
GROUP BY CONVERT(date, [p].[CreatedTime])
) AS [t]
LEFT JOIN (
SELECT CONVERT(date, [p0].[CreatedTime]) AS [c], [t].[c0]
FROM [PartnerInvitation] AS [p0]
WHERE [p0].[Accepted] = 1
GROUP BY CONVERT(date, [p0].[CreatedTime])
) AS [t0] ON [t].[c] = [t0].[c]
ORDER BY (SELECT 1)
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY',N'@__p_0 int,@__p_1 int',@__p_0=0,@__p_1=10
The above query gives a following error:
Msg 4104, Level 16, State 1, Line 9. The multi-part identifier "t.c0" could not be bound.
I expect the second select statement in left join would be SELECT ..., COUNT(*) as [c0] instead of SELECT ..., t.[c0]
Any idea what happened here and how to solve it?