Left Outer Join
List<ServicePacksDTO> allServicePacks = new List<ServicePacksDTO>();
using (var db = new DataContext())
{
allServicePacks=(
from sp in db.ServicePacks
join st in db.States.DefaultIfEmpty() on sp.State_id equals st.State_Id
join type in db.ServiceTypes on sp.ServiceType_Id equals type.ServiceType_Id
where
(type.ServiceType_desc.ToLower() == "accepted")
orderby sp.AustState_id
select sp.ToServicePacksDTO(db)).ToList();
}
The current code works fine, until开发者_如何学Python I attempt to do an outer join on state. Is there away to do this easily?
Well firstly, you need to provide a bit more detail that "works fine, until i attempt to do xx".
What doesn't work? Is there an error? Unexpected results?
Forgetting about the DTO projection and db.ServiceTypes
join, to do a LOJ between db.ServicePacks
and db.States
, do this:
var x = (from sp in db.ServicePacks
join st in db.States on sp.State_id equals st.State_id into spst
from x in spst.DefaultIfEmpty()
select new { /* fields */ }
).ToList();
Try that first, make sure that works (it should), and then add on your other join and projection.
精彩评论