1

私のjoin linq式は次のようなものです

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();

上記の左外部結合を実装する方法は?

4

3 に答える 3

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();
于 2012-12-29T16:52:38.417 に答える
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"
                             });
于 2012-12-29T07:10:52.437 に答える