我有一類項目作爲合併自定義對象的多個列表的內容 - C#
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
包含Person
對象與專案編號和項目名。
ProjectId =1, ProjectName = "X", Customer = null, Address = null
ProjectId =2, ProjectName = "Y", Customer = null, Address = null
lst2
包含Person
對象與專案編號和客戶
ProjectId =1,ProjectName = null, Customer = "c1", Address = null
ProjectId =2,ProjectName = null, Customer = "c2", Address = null
和
lst3
包含Person
對象與專案編號和地址
ProjectId = 2, ProjectName = null, Customer =null, Address = "a2"
。
考慮到有多個這樣的記錄在每個列表和專案是潮頭對於每一個項目,我怎麼能合併/組合這些列表以獲得一個列表與合併對象
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?
謝謝。
在每個列表,是否有總是(除了專案編號)精確一個屬性不是null? – phg
是否有一套固定的屬性,事先已知? – phg
@phg:兩個問題都沒有。這僅僅是一個例子。 而且列表中可能不包含相同數量的記錄。 – quitprog