我試圖將我的T-SQL轉換爲LINQ。這給了我一個真正的頭痛。任何人都可以指出我在做什麼錯在這裏:使用實體框架將T-SQL轉換爲LINQ
T-SQL
select
h.DateStamp
, h.Machine
, h.SalesInvoice
, h.Status
, sum(i.Quantity) totalquantity
, sum(IIF(h.[Status]='SALES', i.Quantity, 0)) as SalesQty
, sum(IIF(h.[Status]='RETURNS', i.Quantity, 0)) as ReturnQty
, sum(IIF(h.[Status]='SALES', i.Quantity*i.Soldsrp , 0)) as SalesGross
, sum(IIF(h.[Status]='RETURNS', i.Quantity*i.Soldsrp, 0)) as ReturnGross
, sum(IIF(h.[Status]='SALES', i.DiscountAmount , 0)) as SalesDiscount
, sum(IIF(h.[Status]='RETURNS', i.DiscountAmount, 0)) as ReturnDiscount
, sum(IIF(h.[Status]='SALES', i.TotalAmount , 0)) as SalesACD
, sum(IIF(h.[Status]='RETURNS', i.TotalAmount , 0)) as ReturnACD
from CSHR_SalesItems i
left join CSHR_SalesHeader h on h.SalesOrderNum = i.SalesOrderNum
where H.DateStamp between '2015-08-20 00:00:00.000' and '2015-08-27 23:59:00.000'
group by h.DateStamp, h.Machine, h.SalesInvoice, h.[Status]
這裏是我試圖把它變成LINQ與實體框架。 GroundCommander是實體
using (var GC = new GroundCommanderEntities())
{
foreach (var item in GC.CSHR_SalesItems)
{
var Summary =
from i in GC.CSHR_SalesItems
join h in GC.CSHR_SalesHeader on item.SalesOrderNum equals h.SalesOrderNum into IH
from h in IH
where (h.DateStamp >= dateTimePickerStart.Value && h.DateStamp <= dateTimePickerLast.Value)
select new {
h.DateStamp,
h.Machine,
h.SalesInvoice,
h.Status,
i.Quantity //need to get the sum
};
}
您需要'從IH.DefaultIfEmpty()'中的h使它成爲左連接。 – juharr
謝謝,我沒注意到。 –
雖然我不認爲你需要一個左連接,因爲當左邊爲空時,哪裏會是假的。 – juharr