在C#中通过linq条件检索数据

时间:2017-04-17 15:10:10

标签: c# linq

            var Getdetails = (from p in XYZDb.tblPulls
                          join
                          ro in XYZDb.tblRentalOrders
                          on p.AffCode equals ro.AffCode.Value
                          join
                          tpsb in XYZDb.tblPullSheetBatchProcessings
                          on p.PullNo.ToString() equals tpsb.PullSheet

                          select new
                          {
                              PullNos = p.PullNo,
                              AffCode = p.AffCode,
                              TotalItems = p.TotalItems,
                              p.PostedOn,
                              p.UpdatedOn,
                              p.IsPrinted,
                              BatchName =  tpsb.BatchName 

                          })

                              .Where(i => i.PostedOn >= from_date && i.PostedOn <= to && i.IsPrinted != null).Distinct();

在上面的代码中,只有具有BatchName的pullno即将到来,我想检索该时区内的所有pullno,如果是批处理,那么BatchName也会出现。我被困在那。任何形式的帮助将不胜感激。随意提出任何问题。

1 个答案:

答案 0 :(得分:0)

使用左外连接,

 var Getdetails = (from p in XYZDb.tblPulls
                      join
                      ro in XYZDb.tblRentalOrders
                      on p.AffCode equals ro.AffCode.Value
                      join
                      tpsb in XYZDb.tblPullSheetBatchProcessings
                      on p.PullNo.ToString() equals tpsb.PullSheet into pt
                      from batch in pt.DefaultIfEmpty()
                      select new
                      {
                          PullNos = p.PullNo,
                          AffCode = p.AffCode,
                          TotalItems = p.TotalItems,
                          p.PostedOn,
                          p.UpdatedOn,
                          p.IsPrinted,
                          BatchName = (batch == null ? String.Empty : batch.BatchName ) 

                      })
                      .Where(i => i.PostedOn >= from_date && i.PostedOn <= to && i.IsPrinted != null).Distinct();