Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用左外联接的多字段LINQ联接查询_C#_.net_Linq - Fatal编程技术网

C# 使用左外联接的多字段LINQ联接查询

C# 使用左外联接的多字段LINQ联接查询,c#,.net,linq,C#,.net,Linq,我的产品表和官员表如下: 产品 官员 我需要将Product表中的3列(Officer1ID、Officer2ID、Officer3ID)与OfficerID合并到Officer表中,以生成如下结果: ProductID | ProductName | Officer1Name | Officer2Name | Officer3Name --------- | ----------- | ------------ | ------------ | ------------ 12

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

产品 官员

我需要将Product表中的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 };
List lstProduct=GetProducts();
List lstOfficer=GetOfficers();
var merge=来自LST产品中的p
从o加入p.Officer1ID等于o.OfficerID
选择新建{ProductID=p.ProductID,ProductName=p.ProductName,OfficerName=o.OfficerName};
编辑

Product表中的OfficeId可以是0(在Officer表中不存在)。

只需应用联接3次(每个
OfficeId一次)


只需应用联接3次(每次一次
OfficeId
):

您可以使用

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

产品

ProductID | ProductName | ProductOfficiersID
--------- | ----------- | ---------- 
12        | Mouse       | 1        
13        | Keyboard    | 2       
生产官员

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

您可以使用

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

产品

ProductID | ProductName | ProductOfficiersID
--------- | ----------- | ---------- 
12        | Mouse       | 1        
13        | Keyboard    | 2       
生产官员

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

我最终使用了子查询,灵感来自于


谢谢你们的帮助

我最终使用了子查询,灵感来自


谢谢你们的帮助

名为
Foo1
Foo2
等的列。。。通常是数据模型错误的迹象。@mark byers:应该支持某种关系表(只是为了补充您的评论)我们的客户有各种产品。不同的产品将有不同的产品专员、财务专员和项目专员负责管理产品。这就是要求。名为
Foo1
Foo2
等列。。。通常是数据模型错误的迹象。@mark byers:应该支持某种关系表(只是为了补充您的评论)我们的客户有各种产品。不同的产品将有不同的产品专员、财务专员和项目专员负责管理产品。这是要求。谢谢你的回答。这是非常接近我要找的。我忘了提到Product表中的一些OfficerIDs为null/空。谢谢你的回答。这是非常接近我要找的。我忘了提到Product表中的一些OfficerIDs是null/空的。
ProductOfficiersID | ProductID | OficierId 
------------------ | --------- | -----------
1                  | 12        | 123     
1                  | 12        | 124  
1                  | 12        | 125
2                  | 13        | 234
...
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()
            };