【发布时间】:2023-03-13 12:15:01
【问题描述】:
我有一个类项目
public class Project
{ public int ProjectId { get; set; }
public string ProjectName { get; set; }
public string Customer { get; set; }
public string Address{ get; set; }
}
我有 3 个列表
List<Project> lst1; List<Project> lst2; List<Project> lst3;
lst1 包含具有 ProjectId 和 ProjectName 的 Person 对象。
ProjectId =1, ProjectName = "X", Customer = null, Address = null
ProjectId =2, ProjectName = "Y", Customer = null, Address = null
lst2 包含带有 ProjectId 和 Customer 的 Person 对象
ProjectId =1,ProjectName = null, Customer = "c1", Address = null
ProjectId =2,ProjectName = null, Customer = "c2", Address = null
, 和
lst3 包含带有 ProjectId 和地址的 Person 对象
ProjectId = 1, ProjectName = null, Customer =null, Address = "a1"
ProjectId = 2, ProjectName = null, Customer =null, Address = "a2".
考虑到每个列表中有多个这样的记录,并且每个项目的 ProjectId 都是唯一的,我如何合并/组合这些列表以获得一个包含合并对象的列表
ProjectId=1, ProjectName="X", Customer="c1", address="a1"
ProjectId=2, ProjectName="Y", Customer="c2", address="a2"
我发现这些链接相似并尝试使用它但无法满足结果
Create a list from two object lists with linq
How to merge two lists using LINQ?
谢谢。
【问题讨论】:
-
在每个列表中,是否总是(除了 projectId)恰好有一个属性不为空?
-
还有预先知道的固定属性集吗?
-
@phg :对这两个问题都没有。这只是一个例子。并且列表可能包含不同数量的记录。