簡體   English   中英

內連接后的左外連接 linq c#

[英]left outer join after a inner join linq c#

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

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

在這種情況下,您可以在 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();

我通過以下方式制作

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"
                             });
var result = from p in db.Products.Where(p => p.IsInActive == false && p.IsLiterature == false)
             join up in db.UserToProducts.Where(up => up.IsInActive == false
             && up.UserID == userID) on p.ProductID equals up.ProductID
             join ul in db.UserToLocations.Where(ul => ul.IsInActive == false)
             on up.UserID equals ul.UserID
             join l in db.Locations.Where(l => l.IsInActive == false)
             on ul.LocationID equals l.LocationID
             join s in db.Sales.Where(s => s.IsInActive == false && s.UserID == userID && s.Date.Month == month
             && s.Date.Year == year && s.WholeSalerID == wholeSalerId) on l.LocationID equals s.LocationID into joined
             from merged in joined.DefaultIfEmpty()
             group new { up.Product, ul.Location, merged }
             by new
             {
                 LocationID = ul.Location.LocationID,
                 ul.Location.Name,
                 ProductID = up.Product.ProductID,
                 ProdictName = up.Product.Name,
                 up.Product.Code,
                 month = merged.Date.Month,
                 year = merged.Date.Year,
                 merged.Date
             } into g
             select new SalesachievementBO()
             {
                 ProductID = g.Key.ProductID,
                 ProductName = g.Key.ProdictName,
                 ProductCode = g.Key.Code,
                 LocationId = g.Key.LocationID,
                 LocationName = g.Key.Name,
                 Qty = g.Sum(p => (p.merged.Qty == null ? 0 : p.merged.Qty)),
                 Price = g.Sum(p => p.merged.Price == null ? 0 : p.merged.Price)
             };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM