1

我的加入 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 回答
0
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)
             };
于 2020-04-12T08:56:57.520 回答