代码之家  ›  专栏  ›  技术社区  ›  bla

使用左外部联接的具有多个字段的Linq联接查询

  •  1
  • bla  · 技术社区  · 15 年前

    我有产品表和官员表如下:

    产品

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

    官员

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



    我需要将产品表中的3列(officer1id、officer2id、officer3id)与officerid合并到officer表中,以生成如下结果:

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



    这是我的尝试。我知道如何加入1个字段,但不是多个字段。有人能帮忙吗?谢谢!

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

    编辑
    产品表中的officerids可以是0(officer表中不存在)。

    3 回复  |  直到 14 年前
        1
  •  4
  •   Yakimych    15 年前

    只需应用连接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
                         };
    
        2
  •  3
  •   Community Mohan Dere    8 年前

    你可以用 multiple joins .

    您应该重新考虑您的数据模型。我建议使用 junction table 要建立多对多关系:

    产品

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

    生产商

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

    官员

        3
  •  0
  •   Community Mohan Dere    8 年前

    我最终使用了子查询,灵感来自 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()
                };
    

    谢谢你们的帮助!

    推荐文章