简体   繁体   English

LINQ连接查询,使用左外部连接具有多个字段

[英]LINQ join query with multiple fields using left outer join

I have Product table and Officer table as follow: 我有产品表和官员表,如下所示:

Product 产品

ProductID | ProductName | Officer1ID | Officer2ID | Officer3ID
--------- | ----------- | ---------- | ---------- | ----------
12        | Mouse       | 123        | 124        | 125
13        | Keyboard    | 234        | 235        | 0

Officer

OfficerID | OfficerName 
--------- | ----------- 
123       | John       
124       | Andy    
125       | Mark



I need to join 3 columns (Officer1ID, Officer2ID, Officer3ID) from Product table with OfficerID in Officer table to produce result like this: 我需要将Product表中的3列(Officer1ID,Officer2ID,Officer3ID)与Officer表中的OfficerID结合起来以产生如下结果:

ProductID | ProductName | Officer1Name | Officer2Name | Officer3Name
--------- | ----------- | ------------ | ------------ | ------------
12        | Mouse       | John         | Andy         | Mark
13        | Keyboard    | Dave         | Fred         | Leon



This is my attempt. 这是我的尝试。 I know how to join 1 field, but not multiple. 我知道如何加入1个字段,但不是多个字段。 Can anyone help? 有人可以帮忙吗? Thanks! 谢谢!

List<Product> lstProduct = GetProducts();

List<Officer> lstOfficer = GetOfficers();

var merge = from p in lstProduct
   join from o in lstOfficers on p.Officer1ID equals o.OfficerID
   select new { ProductID = p.ProductID, ProductName = p.ProductName, OfficerName = o.OfficerName };

EDIT 编辑
OfficerIDs in Product table could be 0(not exist in Officer table). 产品表中的OfficerID可以为0(在Officer表中不存在)。

Just apply the join 3 times (once for each OfficerID ): 只需将OfficerID应用3次(每个OfficerID一次):

var merge = from p in lstProduct
            join o1 in lstOfficer on p.Officer1ID equals o1.OfficerID
            join o2 in lstOfficer on p.Officer2ID equals o2.OfficerID
            join o3 in lstOfficer on p.Officer3ID equals o3.OfficerID
            select new
                     {
                         ProductID = p.ProductID,
                         ProductName = p.ProductName,
                         Officer1Name = o1.OfficerName,
                         Officer2Name = o2.OfficerName,
                         Officer3Name = o3.OfficerName
                     };

You can do this using multiple joins . 您可以使用多个联接来执行此操作。

You should reconsider your data model. 您应该重新考虑数据模型。 I suggest using junction table to make many to many relationship: 我建议使用联结表建立多对多关系:

Product 产品

ProductID | ProductName | ProductOfficiersID
--------- | ----------- | ---------- 
12        | Mouse       | 1        
13        | Keyboard    | 2       

ProductOfficiers 产品官

ProductOfficiersID | ProductID | OficierId 
------------------ | --------- | -----------
1                  | 12        | 123     
1                  | 12        | 124  
1                  | 12        | 125
2                  | 13        | 234
...

Officer ... 官员 ...

I ended up using subquery, inspired from this thread . 我最终使用了受此线程启发的子查询。

var merge = from p in lstProduct
            select new
            {
                p.ProductID,
                p.ProductName,
                Officer1Name = (from o in lstOfficer
                               where o.OfficerID == p.Officer1ID
                               select o.OfficerName).FirstOrDefault(),
                Officer1Name = (from o in lstOfficer
                               where o.OfficerID == p.Officer2ID
                               select o.OfficerName).FirstOrDefault(),
                Officer2Name = (from o in lstOfficer
                               where o.OfficerID == p.Officer3ID
                               select o.OfficerName).FirstOrDefault()
            };

Thanks guys for your help! 谢谢大家帮助!

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

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