2017-06-21 288 views
0

下面是我的SQL查詢左外連接,並從三個表拿到三列

SELECT (((((('L' + (CONVERT(NVARCHAR(MAX), t0.Length))) + 'W') + (CONVERT(NVARCHAR(MAX), t0.Width))) + 
       'H') + (CONVERT(NVARCHAR(MAX), t0.Height))) + ' - ') + t0.DimUoM AS 'Dimensions' 
     , (((CONVERT(NVARCHAR(MAX), t0.NetWt)) + ' (') + t0.NetWtUoM) + ')' AS 'Weight' 
     , t0.UPCCode 
     , t1.DisplayBrand AS 'Brand' 
     , t2.ID 
FROM SmartSearchNewQA.dbo.MaterialMaster AS t0 
LEFT OUTER JOIN dbo.BrandConversion AS t1 
     ON t0.Brand = t1.SAPBrand 
LEFT OUTER JOIN ClickCommerce.dbo.tblWarrantyPartReturn AS t2 
     ON t0.MaterialNumber = t2.PartNumber 
LEFT OUTER JOIN ClickCommerce.dbo.tblWarrantyPartReturnAddress AS t3 
     ON t2.ReturnCode = t3.ReturnCode 
WHERE t0.MaterialNumber = 'S1-326000' AND 
     t2.DistributorId = '00100'; 

Query Result

正在嘗試上述查詢LINQ轉換並不能映射第二個表。請找到下面的LINQ我推導到目前爲止

from mm in ctx.MaterialMasters 
join bc in ctx.BrandConversions on mm.Brand equals bc.SAPBrand into bc1 
from brandConv in bc1.DefaultIfEmpty() 
join wp1 in ctx.tblWarrantyPartReturns 
    on mm.MaterialNumber equals wp1.PartNumber into bc2 
from partconv in bc2.DefaultIfEmpty() 
join wp2 in ctx.tblWarrantyPartReturnAddresses 
    on bc2 
from partconv2 in bc3.DefaultIfEmpty() 
where mm.MaterialNumber == materialNumber 
select new MaterialHeader 
{ 
    Brand = brandConv.DisplayBrand, 
    Dimensions = "L" + mm.Length + ":W" + mm.Width + ":H" + mm.Height + " - " + mm.DimUoM, 
    UPCCode = mm.UPCCode, 
    Weight = mm.NetWt + " (" + mm.NetWtUoM + ")" 
}) 

請幫助我與LINQ查詢到達上述SQL結果。

謝謝

+0

哎呀,該查詢是可怕的閱讀。所有這些[和]絕對無用的別名使得這個用戶充其量是充滿敵意的。然後是瘋狂的多餘的(和)。我知道你試圖將它轉移到LINQ,但你應該閱讀更多關於爲什麼這些別名是壞的。 http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-table-aliases-like-a-b-c-or-t1-t2-t3.aspx –

回答

0

這是太長的評論,它不會格式正常。以下是使用有意義的別名查看相同的查詢的情況。我也刪除了所有的redunant括號和方括號。這很容易看到我在做什麼。

SELECT 'L' + CONVERT(NVarChar(MAX), mm.Length) + 'W' + CONVERT(NVarChar(MAX), mm.Width) + 'H' + CONVERT(NVarChar(MAX), mm.Height) + ' - ' + mm.DimUoM AS Dimensions 
    , CONVERT(NVarChar(MAX), mm.NetWt) + ' (' + mm.NetWtUoM + ')' AS Weight 
    , mm.UPCCode 
    , bc.DisplayBrand AS Brand 
    , wpr.ID 
FROM SmartSearchNewQA.dbo.MaterialMaster AS mm 
LEFT OUTER JOIN dbo.BrandConversion AS bc ON mm.Brand = bc.SAPBrand 
LEFT OUTER JOIN ClickCommerce.dbo.tblWarrantyPartReturn as wpr on mm.MaterialNumber = wpr.PartNumber 
LEFT OUTER JOIN ClickCommerce.dbo.tblWarrantyPartReturnAddress as wpra on wpr.ReturnCode = wpra.ReturnCode 
WHERE mm.MaterialNumber = 'S1-326000' 
    AND wpr.DistributorId ='00100' 
1

我會嘗試這樣的事:

from t0 in ctx.MaterialMasters 
join t1 in ctx.BrandConversions 
    on t0.Brand equals t1.SAPBrand into firstLeftJoin 
from m in firstLeftJoin.DefaultIfEmpty() 
join t2 in ctx2.tblWarrantyPartReturns 
    on t2.PartNumber equals m.MaterialNumber into secondLeftJoin 
from n in secondLeftJoin.DefaultIfEmpty() 
join t3 in ctx2.tblWarrantyPartReturnAddress 
    on t3.ReturnCode equals n.ReturnCode into thirdLeftJoin 
from s in thirdLeftJoin.DefaultIfEmpty() 
where t0.MaterialNumber = "S1-326000" && 
    t2.DistributorId = "00100" 
select new MaterialHeader { 
          Brand = t1.DisplayBrand, 
          Dimensions = "L" + t0.Length + ":W" + t0.Width + ":H" + t0.Height + " - " + t0.DimUoM, 
          UPCCode = t0.UPCCode, 
          Weight = t0.NetWt + " (" + t0.NetWtUoM + ")" 
          } 

這引起了我的注意,你有兩個數據庫:SmartSearchNewQA和ClickCommerce。在你的SQL查詢中,所以我使用了兩個數據庫上下文:ctx和ctx2。假設你將每個數據庫分開。

您還可以嘗試LINQPad來開發LINQ技能。