Let me just preface this with acknowledging that this bug no longer occurs with newer versions of EFCore (5+), but unfortunately we cannot update our project right now.
I created a sample code in order to better show how this bug happens. In this sample code I have a DbContext with three tables: Companies, Locations and Employees. A company can have 1 or more employees, and 0 or more locations. I want to create a query of companies and locations that would return like this:
Company | Location | Employees
-----------------------------------------------
Company 1 | Location 1 | Employee 1, Employee 2
Company 1 | Location 2 | Employee 1, Employee 2
Company 2 | | Employee 3, Employee 4
Different locations for the same company should be in separate lines, and if the company doesn't have a location, it should still show up.
I came up with the following query using Linq, and it does what I described above in EFCore 5+, but not in 3.1.11
(from c in ctx.Companies
join _l in ctx.Locations on c.Id equals _l.CompanyId into lGroup
from l in lGroup.DefaultIfEmpty()
where
c.Id == new Guid("FFF461F3-9E38-4DA0-8B31-AC4D0C90A4D0") // Id of Company 1 in the code block above
select new
{
CompanyName = c.Name,
LocationName = l == null ? string.Empty : l.Name,
Employees = c.Employees.Select(e => e.Name)
}).AsNoTracking().ToList();
In EFCore 5+ this returns two items in the resulting list, exactly like I wanted. But in 3.1.11 it returns only one line, with the first location. The second line with the second location is missing.
The thing is, if you change the query to not use the DefaultIfEmpty() (basically changing it from a left join to an inner join) it works exactly as intended, but in that case if you try querying a company with no locations, it won't show up (since it's an inner join with location).
Does anyone know if there's a way I could achieve this in an efficient manner?
Thank you in advance.
EDIT: This is the generated query:
SELECT [c].[Name], CASE
WHEN [l].[Id] IS NULL THEN N''
ELSE [l].[Name]
END, [c].[Id], [e].[Name], [e].[Id]
FROM [Companies] AS [c]
LEFT JOIN [Locations] AS [l] ON [c].[Id] = [l].[CompanyId]
LEFT JOIN [Employees] AS [e] ON [c].[Id] = [e].[CompanyId]
WHERE [c].[Id] = 'f2c44d70-d713-42dc-b92a-e93b175fc351'
ORDER BY [c].[Id], [e].[Id]