I've got the following structure
Teams >- Membership -< Employees
Membership is a many to many relation table with extra fields StartDate and EndDate, which is set up per this example: Create code first, many to many, with additional fields in association table.
What I'm trying to accomplish is to get in one Linq query the all the Teams (left outer join), and Employees (left/right outer join), basically resulting in a big list with teams (and coupled employees if any) and employees (with coupled teams if any).
Is this possible?
I'm trying to do it via the following but not getting there completely:
var queryable = (from t in Context.Teams
join m in Context.Membership on t.Id equals m.TeamId into tm
from result in tm.DefaultIfEmpty()
join e in Context.Employees on result.EmployeeId equals e.Id into em
from r in em.DefaultIfEmpty()
select new { TeamLinq = t, MembershipLinq = result }).ToList();
or
var q = (from m in Context.Membership
join t in Context.Teams on m.TeamId equals t.Id into tm
join e in Context.Employees on m.EmployeeId equals e.Id into em
from result in tm.DefaultIfEmpty()
from result2 in em.DefaultIfEmpty()
select new { Foo = result, Bar = result2 }).ToList();
but not getting what I'm after.
In simple SQL I would write it like
select * from Teams t left outer join Membership m on t.Id = m.TeamId
right outer join Employees e on e.Id = m.EmployeeId