Hello A Volkov.
I'll try to explain what happening in this three cases.
Good Work case: In this case ewc.Car.Id it just a column and your query translates to pure left join statement. Query translates to
SELECT COUNT_BIG(*) AS [c01umn] FROM [dbo].[Employee] [a] LEFT OUTER JOIN [dbo].[EmployeeWithCar] [b] ON ([a].[Id] = [b].[Id]);
So you can receive null in field Car = ewc.Car.Id, And DO just take it and do nothing with it because it understand that is nature of left join.
Workaround case: Query translates to
SELECT COUNT_BIG(*) AS [c01umn] FROM [dbo].[Employee] [a] LEFT OUTER JOIN [dbo].[EmployeeWithCar] [b] ON ([a].[Id] = [b].[Id]) LEFT OUTER JOIN [dbo].[Car] [c] ON ([b].[Car.Id] = [c].[Id]);
In this case you select CarId = ewc.Car.Id in first left join (see Good Work case) and put result to next left join. CarId field is nullable field. If CarId is null then Car in second Left join is null. As in previous case DO does nothing because it understand that is left join and fields can be null.
Bad Work Case:
Query translates to
SELECT COUNT_BIG(*) as [c01umn] FROM [dbo].[Employee] [a] LEFT OUTER JOIN [dbo].[EmployeeWithCar] [b] ON ([a].[Id] = [b].[Id]) INNER JOIN [dbo].[Car] [c] ON ([b].[Car.Id] = [c].[Id]);
In this case you select Car= ewc.Car.This is not simple column, this is row in different table and DO must create another join. Besides, this field marked by [Field(Nullable = false)] which tells DO that this field must be not null. And DO try to guarantee that all rows in result of query contain not null Car object. So we have one row in result of query execution.
So if you need to select all three rows than you need manually write second left join as in workaround case.
answered
Sep 04 '14 at 04:10
Alexey Kulakov
772●2●5