Linq to Entities - left Outer Join
Could you please help me to figure this one out? 开发者_运维百科I need to replace a join with OSLP table with OUTER join. Seems a bit tricky for someone who is not an expert in Linq to entities. How would I do that?
var surgeonList = (
from item in context.T1_STM_Surgeon
.Include("T1_STM_SurgeonTitle")
.Include("OTER")
where item.ID == surgeonId
join reptable in context.OSLP
on item.Rep equals reptable.SlpCode
select new
{
ID = item.ID,
First = item.First,
Last = item.Last,
Rep = reptable.SlpName,
Reg = item.OTER.descript,
PrimClinic = item.T1_STM_ClinicalCenter.Name,
Titles = item.T1_STM_SurgeonTitle,
Phone = item.Phone,
Email = item.Email,
Address1 = item.Address1,
Address2 = item.Address2,
City = item.City,
State = item.State,
Zip = item.Zip,
Comments = item.Comments,
Active = item.Active,
DateEntered = item.DateEntered
}).ToList();
Thanks in advance!!
Syntax is as follows and let me know if you have trouble translating your code. Generic Outer Join (group join):
var query = from l in left
join r in right
on l.ID
equals l.right.ID into groupedJoin
select new
{
ID= l.ID,
OuterJoined= groupedJoin.Select(r=> right)
};
Your result is everything in left even if right doesn't exist.
Obviously i can't guarantee it will compile but it would look like this:
var surgeonList = (
from item in context.T1_STM_Surgeon
.Include("T1_STM_SurgeonTitle")
.Include("OTER")
where item.ID == surgeonId
join reptable in context.OSLP
on item.Rep equals reptable.SlpCode into groupedJoin
select new
{
ID = item.ID,
First = item.First,
Last = item.Last,
Rep = reptable.SlpName,
Reg = item.OTER.descript,
PrimClinic = item.T1_STM_ClinicalCenter.Name,
Titles = item.T1_STM_SurgeonTitle,
Phone = item.Phone,
Email = item.Email,
Address1 = item.Address1,
Address2 = item.Address2,
City = item.City,
State = item.State,
Zip = item.Zip,
Comments = item.Comments,
Active = item.Active,
DateEntered = item.DateEntered
OSLP = groupedJoin.Select(x=>WHATEVERYOUNEED)
}).ToList();
And you just need to replace groupedJoin.Select(x=>WHATEVERYOUNEED) with either selecting something or a simple .ToList
精彩评论