LEFT OUTER JOIN in LINQ

后端 未结 22 2450
臣服心动
臣服心动 2020-11-21 04:49

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Corr

22条回答
  •  渐次进展
    2020-11-21 05:25

    If you need to join and filter on something, that can be done outside of the join. Filter can be done after creating the collection.

    In this case if I do this in the join condition I reduce the rows that are returned.

    Ternary condition is used (= n == null ? "__" : n.MonDayNote,)

    • If the object is null (so no match), then return what is after the ?. __, in this case.

    • Else, return what is after the :, n.MonDayNote.

    Thanks to the other contributors that is where I started with my own issue.


            var schedLocations = (from f in db.RAMS_REVENUE_LOCATIONS
                  join n in db.RAMS_LOCATION_PLANNED_MANNING on f.revenueCenterID equals
    
                      n.revenueCenterID into lm
    
                  from n in lm.DefaultIfEmpty()
    
                  join r in db.RAMS_LOCATION_SCHED_NOTE on f.revenueCenterID equals r.revenueCenterID
                  into locnotes
    
                  from r in locnotes.DefaultIfEmpty()
                  where f.LocID == nLocID && f.In_Use == true && f.revenueCenterID > 1000
    
                  orderby f.Areano ascending, f.Locname ascending
                  select new
                  {
                      Facname = f.Locname,
                      f.Areano,
                      f.revenueCenterID,
                      f.Locabbrev,
    
                      //  MonNote = n == null ? "__" : n.MonDayNote,
                      MonNote = n == null ? "__" : n.MonDayNote,
                      TueNote = n == null ? "__" : n.TueDayNote,
                      WedNote = n == null ? "__" : n.WedDayNote,
                      ThuNote = n == null ? "__" : n.ThuDayNote,
    
                      FriNote = n == null ? "__" : n.FriDayNote,
                      SatNote = n == null ? "__" : n.SatDayNote,
                      SunNote = n == null ? "__" : n.SunDayNote,
                      MonEmpNbr = n == null ? 0 : n.MonEmpNbr,
                      TueEmpNbr = n == null ? 0 : n.TueEmpNbr,
                      WedEmpNbr = n == null ? 0 : n.WedEmpNbr,
                      ThuEmpNbr = n == null ? 0 : n.ThuEmpNbr,
                      FriEmpNbr = n == null ? 0 : n.FriEmpNbr,
                      SatEmpNbr = n == null ? 0 : n.SatEmpNbr,
                      SunEmpNbr = n == null ? 0 : n.SunEmpNbr,
                      SchedMondayDate = n == null ? dMon : n.MondaySchedDate,
                      LocNotes = r == null ? "Notes: N/A" : r.LocationNote
    
                  }).ToList();
                    Func LambdaManning = (x) => { return x == 0 ? "" : "Manning:" + x.ToString(); };
            DataTable dt_ScheduleMaster = PsuedoSchedule.Tables["ScheduleMasterWithNotes"];
            var schedLocations2 = schedLocations.Where(x => x.SchedMondayDate == dMon);
    

提交回复
热议问题