Left outer joins in LINQ are not so obvious in comparison to plain old SQL.
The following shows how an out join can be costucted in LINQ
var query = (from e in dc.GetTable<Employee>()
join ea in dc.GetTable<EmployeeAddress>()on e.Id equals ea.EmployeeId into tempAddresses
from addresses in tempAddresses.DefaultIfEmpty()
select new { e.FirstName, e.LastName, addresses.State, addresses.Town});
join ea in dc.GetTable<EmployeeAddress>()on e.Id equals ea.EmployeeId into tempAddresses
from addresses in tempAddresses.DefaultIfEmpty()
select new { e.FirstName, e.LastName, addresses.State, addresses.Town});
The SQL equivalent would be:
select [t0].[FirstName], [t0].[LastName], [t1].[State] as[State], [t1].[Town]as [Town]
from [dbo].[Employee] as [t0
Left outer join [dbo].[EmployeeAddress]as[t1] on[t0].[Id] = [t1].[PersonID]
from [dbo].[Employee] as [t0
Left outer join [dbo].[EmployeeAddress]as[t1] on[t0].[Id] = [t1].[PersonID]
No comments:
Post a Comment