无法合并2个列表并形成一个带有两年标记的主列表
本文关键字:列表 两年 2个 合并 一个 | 更新日期: 2023-09-27 18:09:12
我有一个列表list1
CurrentYearDate Marks1 LastYearDate Marks2
09/01/2012 200
09/02/2012 10
09/03/2012 30
09/04/2012 40
我有一个列表list2
CurrentYearDate Marks1 LastYearDate Marks2
9/01/2011 10
09/02/2011 10
09/03/2011 30
09/04/2011 40
我需要合并两个列表在一起列表主我有一个列表list1
CurrentYearDate Marks1 LastYearDate Marks2
09/01/2012 200 9/01/2011 10
09/02/2012 10 09/02/2011 10
09/03/2012 30 09/03/2011 30
09/04/2012 40 09/04/2011 40
如果索引对齐,如您的示例中所示,因此每个列表中的第一项相互对应,那么您可以使用Zip
:
var mergedLists = list1.Zip(list2, (a,b)=> new YourClassName()
{
CurrentYearDate = a.CurrentYearDate,
Marks1 = a.Marks1,
LastYearDate = b.LastYearDate,
Marks2 = b.Mark2,
});
如果索引不对应,则需要使用Join
:
var joinedLists = list1.Join(list2, curr => curr.CurrentYearDate, last => last.LastYearDate.AddYears(1), (a,b)=> new YourClassName()
{
CurrentYearDate = a.CurrentYearDate,
Marks1 = a.Marks1,
LastYearDate = b.LastYearDate,
Marks2 = b.Mark2,
});