对LINQ的SQL查询未获得相同结果

时间:2020-06-24 17:52:20

标签: c# sql-server linq

我将下面的SQL查询转换为LINQ,我遇到的问题是,当我运行SQL查询时,我得到了50的计数,但是当我运行linq代码时,我得到了42(两者都在同一个DB上运行) 。我想念什么吗?除此之外,我该如何验证总和大小写?

SQL查询

Select  oh.Company,  Sum(Case When (vTS.TotPrice) is null then oh.OrderAmt else (oh.OrderAmt - vTS.TotPrice) End) as TotOpen  from dbo.OrderHed as oh inner join dbo.Customer as cust on oh.Company = cust.Company and oh.CustNum = cust.CustNum left outer join dbo.vTotalShippedByOrderNum as vTS on oh.Company = vTS.Company and oh.CustNum = vTS.CustNum and oh.OrderNum = vTS.OrderNum where  oh.Company = 'MyComp' and oh.VoidOrder = 0 AND oh.OpenOrder = 1 AND  oh.ReadyToFulfill = 1 and oh.OrderHeld = 0 and (cust.CreditHold = 0 or oh.CreditOverride = 1) and (cust.CheckBox07 = 0 and cust.CheckBox08 = 0 or oh.Checkbox04 = 1) and oh.RequestDate <  '03/01/2020' group by oh.Company

vTotalShippedByOrderNum自定义视图

SELECT TOP (100) PERCENT Company, CustNum, OrderNum, ROUND(SUM(Number06), 2) AS TotPrice, ROUND(SUM(Number07), 4) AS TotCost FROM dbo.ShipDtl WHERE (ReadyToInvoice = 1) GROUP BY Company, CustNum, OrderNum

LINQ代码

var orderR = from oh in Db.OrderHed.With<Erp.Tables.OrderHed>(LockHint.NoLock)
         join cust in Db.Customer.With<Erp.Tables.Customer>(LockHint.NoLock) on new { oh.Company, oh.CustNum } equals new { cust.Company, cust.CustNum }
         join ts in (from sd in Db.ShipDtl.With<Erp.Tables.ShipDtl>(LockHint.NoLock)
                     where sd.Company.Equals(Session.CompanyID) && sd.ReadyToInvoice == true
                     group sd by new
                     {
                     sd.Company,
                                     sd.OrderNum,
                                     sd.CustNum,
                                 } into grpSD
                                 select new
                                 {
                                     Company = grpSD.Key.Company,
                                     OrderNum = grpSD.Key.OrderNum,
                                     CustNum = grpSD.Key.CustNum,
                                     totPrice = Math.Round(grpSD.Sum(x => x.Number06), 2),
                                     totCost = Math.Round(grpSD.Sum(x => x.Number07), 4),
                                 }) on new { oh.Company, oh.OrderNum, oh.CustNum } equals new { ts.Company, ts.OrderNum, ts.CustNum }
                     where oh.Company.Equals(Session.CompanyID) && oh.VoidOrder == false && oh.OpenOrder == true && oh.ReadyToFulfill == true && oh.OrderHeld == false &&
                           (cust.CreditHold == false || oh.CreditOverride == true) && (cust.CheckBox07 == false && cust.CheckBox08 == false || oh.CheckBox04 == true) &&
                           oh.RequestDate < Convert.ToDateTime("03/01/2020")
                     group oh by new { oh.Company, ts.totPrice } into grpOrders
                     select new
                     {   
                        resultSum = string.IsNullOrEmpty(grpOrders.Key.totPrice.ToString()) ? grpOrders.Sum(x => x.OrderAmt) : grpOrders.Sum(x => x.OrderAmt - grpOrders.Key.totPrice),
                        resultCount = grpOrders.Count(),                            
                     };

谢谢

0 个答案:

没有答案