假设我有一个列表,其中包含多个子列表。我想要从给定的列表中创建一个新列表,使其元素按照下面的示例顺序排列。
输入:-
谢谢。
输入:-
List<List<int>> l = new List<List<int>>();
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
List<int> b = new List<int>();
b.Add(11);
b.Add(12);
b.Add(13);
b.Add(14);
b.Add(15);
b.Add(16);
b.Add(17);
b.Add(18);
l.Add(a);
l.Add(b);
输出(列表):
1
11
2
12
3
13
4
14
15
16
输出列表必须不超过10个
元素。
我目前是使用while内的foreach
来实现这一点,但我想知道如何使用LINQ
来实现。
int loopCounter = 0,index=0;
List<int> o=new List<int>();
while(o.Count<10)
{
foreach(List<int> x in l)
{
if(o.Count<10)
o.Add(x[index]);
}
index++;
}
谢谢。
Zip()
和Take()
函数。 - DidoZipLongest
- https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/ZipLongest.cs 。 - mjwills