2012-12-29 102 views
1

我加入LINQ表達式是像follwowing左外連接後,內部聯接LINQ C#

var kycKist = (from aloc in this._classesDataContext.tblUsers 
         join sup in this._classesDataContext.BR_Supervisors on aloc.SupId equals sup.Id 
         where 
          (aloc.UserTypesId == 1 && 
          ((aloc.CreatedOn <= attendanceDate && aloc.ModifyOn >= attendanceDate && 
           aloc.Active == false) || 
          (aloc.Active == true && aloc.CreatedOn <= attendanceDate && 
           aloc.ModifyOn <= attendanceDate))) 
         select 
          new 
          { 
           sup.Name, 
           sup.Region, 
           sup.Area, 
           sup.Distribution_Name, 
           sup.BR_Alloc, 
           sup.Active 
          }).ToList(); 

現在我想使外與上面的代碼

left outer join atn in this._classesDataContext.BR_Attendence on sup.ID equals atn.SupId where atn.date =attendanceDate 

我的代碼草案將加入看起來像這樣

var kycKist = (from aloc in this._classesDataContext.tblUsers 
         join sup in this._classesDataContext.BR_Supervisors on aloc.SupId equals sup.Id 

left outer join atn in this._classesDataContext.BR_Attendence on sup.ID equals atn.SupId where atn.date =attendanceDate 
         where 
          (aloc.UserTypesId == 1 && 
          ((aloc.CreatedOn <= attendanceDate && aloc.ModifyOn >= attendanceDate && 
           aloc.Active == false) || 
          (aloc.Active == true && aloc.CreatedOn <= attendanceDate && 
           aloc.ModifyOn <= attendanceDate))) 
         select 
          new 
          { 
           Present=(atn!=null)?atn.PresentBR:0, 
           sup.Name, 
           sup.Region, 
           sup.Area, 
           sup.Distribution_Name, 
           sup.BR_Alloc, 
           sup.Active 
          }).ToList(); 

如何實現上面的左外連接?

+0

所以我想問題是「我如何做一個與LINQ左加入」? – usr

回答

0

我使其通過以下方式

var filterBrAttendence = (from atn in this._classesDataContext.BR_Attendances 
            where atn.AttenDate == attendanceDate 
            select new {atn.SupId, atn.PresentBR}); 
     var kycKist = (from aloc in this._classesDataContext.tblUsers 
         join sup in this._classesDataContext.BR_Supervisors on aloc.SupId equals sup.Id 
         where 
          (aloc.UserTypesId == 1 && 
          ((aloc.CreatedOn <= attendanceDate && aloc.ModifyOn >= attendanceDate && 
           aloc.Active == false) || 
          (aloc.Active == true && aloc.CreatedOn <= attendanceDate && 
           aloc.ModifyOn <= attendanceDate))) 
         select 
          new 
           { 
            sup.Id, 
            sup.Name, 
            sup.Region, 
            sup.Area, 
            sup.Distribution_Name, 
            sup.BR_Alloc, 
            sup.Active 
           }); 
     var final = (from a in kycKist 
        join b in filterBrAttendence on a.Id equals b.SupId into outer 
        from x in outer.DefaultIfEmpty() 
        select 
         new 
          { 
           a.Name, 
           //a.Region, 
           a.Area, 
           a.Distribution_Name, 
           a.BR_Alloc, 
           a.Active, 
           PresentBR = (x!=null)?x.PresentBR.ToString():"Absent" 
          }); 
1

您可以執行之前或在這種情況下,where子句後的外連接。

var kycKist = (from aloc in this._classesDataContext.tblUsers 
       join sup in this._classesDataContext.BR_Supervisors on aloc.SupId equals sup.Id 
       where 
        (aloc.UserTypesId == 1 && 
        ((aloc.CreatedOn <= attendanceDate && aloc.ModifyOn >= attendanceDate && 
         aloc.Active == false) || 
        (aloc.Active == true && aloc.CreatedOn <= attendanceDate && 
         aloc.ModifyOn <= attendanceDate))) 
       join b in filterBrAttendence on sup.Id equals b.SupId into outer 
       from x in outer.DefaultIfEmpty() 
       select 
        new 
        { 
         sup.Name, 
         sup.Region, 
         sup.Area, 
         sup.Distribution_Name, 
         sup.BR_Alloc, 
         sup.Active, 
         PresentBR = (x != null) ? x.PresentBR.ToString() : "Absent" 
        }).ToList();