简体   繁体   English

C#Linq Lambda左外部联接

[英]C# Linq Lambda Left Outer Join

I need to create a LEFT OUTER JOIN in linq lambda syntax. 我需要使用linq lambda语法创建一个LEFT OUTER JOIN The SQL I am trying to create a linq equivalent of is: 我尝试创建等效于linq的SQL是:

SELECT DISTINCT 
    p.PartNum AS PartNum, p.ShortChar01 AS SkuType, 
    vv.VendorID AS VendorCode, 
    p.PartDescription AS Description, p.Company AS Company
FROM 
    Part p WITH (NOLOCK) 
INNER JOIN
    PartPlant pp ON p.Company = pp.Company AND p.PartNum = pp.PartNum 
LEFT OUTER JOIN
    Vendor vv On pp.VendorNum = vv.VendorNum 
WHERE 
    p.RefCategory = @refCategory

So as you can see its a fairly simple query joining a few tables. 因此,如您所见,它是一个连接几个表的相当简单的查询。 The issue is that it could happen that there is no vendor but we still want the rest of the information hence the left outer join. 问题在于可能没有供应商,但我们仍然需要其余信息,因此需要左外部联接。

My current attempt to recreate this is: 我目前尝试重新创建它是:

_uow.PartService
    .Get()
    .Where(p => p.RefCategory.Equals(level2))
    .Join(_uow.PartPlantService.Get(),
          p => new { p.PartNum, p.Company },
          pp => new { pp.PartNum, pp.Company },
          (p, pp) => new { Part = p, PartPlant = pp })
    .GroupJoin(_uow.VendorService.Get(),
               pprc => pprc.PartPlant.VendorNum,
               v => v.VendorNum,
               (pprc, v) => new { PPRC = pprc, V = v });

I am aware that the select isn't returning the same fields at the moment. 我知道选择当前不返回相同的字段。 I have ignored that for now as I am trying to ensure i am getting the correct values first. 我现在暂时忽略了这一点,因为我试图确保自己首先获得正确的值。

The SQL query returns 41 records with 1 record having a null vendor. SQL查询返回41条记录,其中1条记录的供应商为空。 The linq query returns 40 records obviously not returning the one with the null vendor. linq查询返回40条记录,显然没有返回空供应商的记录。 I have tried using GroupJoin() and DefaultIfEmpty() but I cannot get it to work. 我尝试使用GroupJoin()DefaultIfEmpty()但无法正常工作。

Any help would be greatly appreciated. 任何帮助将不胜感激。

From the comment and links from user2321864, I managed to get it working as follows: 从user2321864的注释和链接中,我设法使其工作如下:

_uow.PartService.Get().Where(p => p.RefCategory.Equals(level2))
                                .Join(_uow.PartPlantService.Get(),
                                        p => new { p.PartNum, p.Company },
                                        pp => new { pp.PartNum, pp.Company },
                                        (p, pp) => new { Part = p, PartPlant = pp })
                                .GroupJoin(_uow.VendorService.Get(),
                                        pprc => pprc.PartPlant.VendorNum,
                                        v => v.VendorNum,
                                        (pprc, v) => new { PPRC = pprc, V = v })
                                .SelectMany(y => y.V.DefaultIfEmpty(),
                                            (x, y) => new { PPRC = x.PPRC, Vendor = y })
                                .Select(r => new Level2Parts()
                                {
                                    CompanyCode = r.PPRC.Part.Company,
                                    Description = r.PPRC.Part.PartDescription,
                                    PartNum = r.PPRC.Part.PartNum,
                                    SkuType = r.PPRC.Part.ShortChar01,
                                    VendorCode = r.Vendor.VendorID
                                })
                                .Distinct();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM