2017-06-14 25 views
-1

我正在轉換舊的webforms應用程序爲asp.net mvc,我有問題將我的一個sql語句轉換爲linq。特別是,我需要幫助分組和連接。我通過查看各種示例嘗試了幾種方法,但都沒有爲我工作。將sql轉換爲linq(連接和分組)

SELECT cp.PartNumber, cp.PartDescription, PFEP.PFEPTx, PFEP.KBQty, 
TX_QOH.QOH, TX_ReworkQOH.Rework_QOH as Rework, SUM(ShippingInput.Qty) AS 
'Ocean' 
FROM CustomerParts as cp 

LEFT JOIN TX_QOH 
ON cp.PartNumber = TX_QOH.PN 

LEFT JOIN TX_ReworkQOH 
ON cp.PartNumber = TX_ReworkQOH.PN 

LEFT JOIN ShippingInput 
ON cp.PartNumber = ShippingInput.PN AND ShippingInput.Status <> 'Received' 

LEFT JOIN PFEP 
ON cp.PartNumber= PFEP.PN 

WHERE cp.PartType = 'Actuator Part' AND cp.Division = 'Bayne' AND cp.Active 
= 'Yes' AND TX_QOH.QOH = '0' 

Group By cp.PartNumber, TX_QOH.QOH, TX_ReworkQOH.Rework_QOH, 
cp.PartDescription, PFEP.PFEPTx, PFEP.KBQty 
Order By cp.PartNumber ASC 
+0

格式化代碼,也許? –

+0

人們更有可能幫助修復企圖翻譯,而不是爲您編寫翻譯 – jhhoff02

+0

*不要*那樣做。這是一個嚴重的錯誤。 LINQ to SQL或LINQ to Entities是ORM的查詢語言,而不是SQL的替代品。不能在沒有ORM的情況下使用LINQ。創建具有關係和導航屬性的適當實體,並讓ORM完成匹配相關行並將它們傳遞給對象的工作 –

回答

0

LINQ版本

var queryNew = (from cp in db.MasterPartLists 
      join tx in db.TxQohs on cp.CustomerPn equals tx.Pn into jTxQoh 
      from tx in jTxQoh.DefaultIfEmpty() 
      join c in db.ShipIns.Where(r => r.ShipInStatusId != 3) on cp.CustomerPn equals c.Pn into jShipIns 
      from c in jShipIns.DefaultIfEmpty() 
      join d in db.Pfeps on cp.CustomerPn equals d.CustomerPn into jPfeps 
      from d in jPfeps.DefaultIfEmpty() 
      where d.PartTypeId == parttype && cp.CustomerDivisionId == division && cp.ActivePartId == 1 && tx.Qoh == 0 
      group new {cp, d, tx, c} by new {cp.CustomerPn, tx.Qoh, cp.PartDescription, d.PfepTx, d.KbQty} 
      into gcp 
      orderby gcp.Key.CustomerPn 
      select new 
      { 
       gcp.Key.CustomerPn, 
       gcp.Key.PartDescription, 
       gcp.Key.PfepTx, 
       gcp.Key.KbQty, 
       gcp.Key.Qoh, 
       Ocean = (int?)gcp.Sum(r => r.c.Qty) 
      }); 

SQL版本

var queryNew = "SELECT cp.CustomerPn, cp.PartDescription, Pfeps.PFEPTx, Pfeps.KBQty, TxQohs.Qoh, SUM(ShipIns.Qty) AS 'Ocean' " 
          + "FROM MasterPartLists as cp " 
          + "LEFT JOIN TxQohs " 
          + "ON cp.CustomerPn = TxQohs.Pn AND TxQohs.Qoh = '0' " 
          + "LEFT JOIN ShipIns " 
          + "ON cp.CustomerPn = ShipIns.Pn AND ShipIns.ShipStatusId <> '3' " 
          + "LEFT JOIN Pfep " 
          + "ON cp.CustomerPn = Pfeps.CustomerPn " 
          + "WHERE cp.PartTypeId = parttype AND cp.CustomerDivisionId = division AND cp.ActivePartId = '1' " 
          + "Group By cp.CustomerPn, TxQohs.Qoh, cp.PartDescription, Pfeps.PfepTx, Pfeps.KbQty " 
          + "Order By cp.CustomerPn ASC "; 
0

這顯然是未經測試,並有可能爲空的問題,試圖時,他們可能不存在由於左連接來訪問字段(如SUM(ShippingInput.Qty)),這就是爲什麼我有些感動的測試以拉姆達Wherejoin

from cp in CustomerParts 
join r_tx_qoh in TX_QOH.Where(r => r.QOH == "0") on cp.PartNumber equals r_tx_qoh.PN into j_tx_qoh 
from r_tx_qoh in j_tx_qoh.DefaultIfEmpty() 
join r_tx_reworkqoh in TX_ReworkQOH on cp.PartNumber equals r_tx_reworkqoh.PN into j_tx_reworkqoh 
from r_tx_reworkqoh in j_tx_reworkqoh.DefaultIfEmpty() 
join r_shippinginput in ShippingInput.Where(r => r.Status != "Received") on cp.PartNumber equals r_shippinginput.PN into j_shippinginput 
from r_shippinginput in j_shippinginput.DefaultIfEmpty() 
join r_pfep in PFEP on cp.PartNumber equals r_pfep.PN into j_pfep 
from r_pfep in j_pfep.DefaultIfEmpty() 
where cp.PartType == "Actuator Part" && cp.Division == "Bayne" && cp.Active == "Yes" 
group new { cp, r_pfep, r_tx_qoh, r_tx_reworkqoh, r_shippinginput } by new { cp.PartNumber, r_tx_qoh.QOH, r_tx_reworkqoh.Rework_QOH, cp.PartDescription, r_pfep.PFEPTx, r_pfep.KBQty } into gcp 
orderby gcp.Key.PartNumber 
select new { gcp.Key.PartNumber, gcp.Key.PartDescription, gcp.Key.PFEPTx, gcp.Key.KBQty, tcp.Key.QOH, Rework = gcp.Key.Rework_QOH, Ocean = gcp.Sum(r => r.r_shippinginput.Qty) } 
+0

感謝NetMage。這讓我非常接近。請在下面找到有效的linq等價物。 – Zhongwenslayer