[英]Merging two Lists into object
我有兩個具有不同屬性的列表。 列表A具有名稱,日期。 列表B具有用戶的三個地址詳細信息,其用戶具有姓,手機號,電子郵件等屬性。我可以將這兩個列表合並為一個類型為UserDetails的對象,其中USerDEtails的模型包括列表A和列表B中的所有上述屬性。
對!
請注意,此示例假定兩個列表的記錄計數相同,並且列表1中的記錄0對應於列表2中的記錄0。
用一個簡單的例子:
public class A
{
public string Name {get; set; }
public DateTime Date {get; set; }
}
public class B
{
public string Surname {get; set;}
public string Mobile {get; set; }
}
public class Combined
{
public string Name {get; set; }
public DateTime Date {get; set; }
public string Surname {get; set;}
public string Mobile {get; set; }
}
List<A> list1 = /*... */;
List<B> list2 = /*... */;
List<Combined> combined = new List<Combined>(list1.Count + list2.Count);
for(int c = 0; c < list1.Count; ++ c)
{
combined.Add(new Combined()
{
Name = list1[c].Name,
Date = list1[c].Date,
Surname = list2[c].Surname,
Mobile = list2[c].Mobile
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.