左外连接并从三个表中获取三列

时间:2017-06-21 20:28:00

标签: c# sql-server linq

以下是我的SQL查询

function penSizeBigger(){
    ctx.lineWidth++;
}

function penSizeSmaller(){
    ctx.lineWidth--;
}

Query Result

AM尝试将上述查询转换为LINQ而无法映射第二个表。请找到目前为止导出的LINQ i

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';

请帮助我使用LINQ查询获得上述SQL结果。

由于

2 个答案:

答案 0 :(得分:0)

这对评论来说太长了,无论如何都不会格式化。以下是使用有意义的别名查找相同的查询。我还删除了所有的冗余括号和方括号。这更容易看到我发生了什么。

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 :(得分:0)

我会尝试这样的事情:

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技能。