I have found a problem when doing an outer join after a nested query. The following is a stripped down way to reproduce it (Northwind):
from ep in db.Employees.Where(f => f.Country == "UK")
join et in db.EmployeeTerritories on ep.EmployeeID equals et.EmployeeID into etg
from et in etg.DefaultIfEmpty()
select ep
This throws
An exception was caught during the execution of a retrieval query: The multi-part identifier "LPLA_2.EmployeeID" could not be bound..
With the following SQL
exec sp_executesql N'
SELECT [LPA_L1].[EmployeeID], [LPA_L1].[LastName], [LPA_L1].[FirstName], [LPA_L1].[Title],
[LPA_L1].[TitleOfCourtesy], [LPA_L1].[BirthDate], [LPA_L1].[HireDate], [LPA_L1].[Address], [LPA_L1].[City], [LPA_L1].[Region],
[LPA_L1].[PostalCode], [LPA_L1].[Country], [LPA_L1].[HomePhone], [LPA_L1].[Extension], [LPA_L1].[Photo], [LPA_L1].[Notes],
[LPA_L1].[ReportsTo], [LPA_L1].[PhotoPath]
FROM
(
(
SELECT [LPLA_1].[EmployeeID], [LPLA_1].[LastName], [LPLA_1].[FirstName],
[LPLA_1].[Title], [LPLA_1].[TitleOfCourtesy], [LPLA_1].[BirthDate], [LPLA_1].[HireDate], [LPLA_1].[Address], [LPLA_1].[City],
[LPLA_1].[Region], [LPLA_1].[PostalCode], [LPLA_1].[Country], [LPLA_1].[HomePhone], [LPLA_1].[Extension], [LPLA_1].[Photo],
[LPLA_1].[Notes], [LPLA_1].[ReportsTo], [LPLA_1].[PhotoPath]
FROM [Northwind].[dbo].[Employees] [LPLA_1]
WHERE
(
(
[LPLA_1].[Country] = @Country1
)
)
) [LPA_L1]
LEFT JOIN [Northwind].[dbo].[EmployeeTerritories] [LPA_L2]
ON [LPLA_2].[EmployeeID] = [LPA_L2].[EmployeeID]
)
',N'@Country1 nvarchar(15)',@Country1=N'UK'
Interestingly, it works if you use an inner join, likewise if you dont do the nested .Where() it runs fine.
Dont you just love Aliases!
Thanks again!