Linq to Entities - Left Outer Join

Could you help me figure this out? I need to replace the connection to the OSLP table with the OUTER connection. It seems a bit complicated for those who are not experts in Linq for entities. How should I do it?

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!

+3
source share
1 answer

The syntax is as follows and let me know if you are having trouble translating the code. Generic Outer 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 the left, even if the right does not exist.

Obviously, I cannot guarantee that it will compile, but it will 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();

groupedJoin.Select(x = > WHATEVERYOUNEED) -, .ToList

0

Source: https://habr.com/ru/post/1738035/


All Articles