[英]How do i convert List<int[]> into List<int>
private static void A(){
List<int[]> list = new List<int[]>();
int[] a = {0,1,2,3,4};
int[] b = {5,6,7,8,9};
list.Add(a);
list.Add(b);
List<int> list2 = new List<int>();
// list2 should contain {0,1,2,3,4,5,6,7,8,9}
}
How would i convert my List<int[]> list
into a List<int>
so that all the int[] arrays
become one giant list of numbers 我将如何将
List<int[]> list
转换为List<int>
以便所有int[] arrays
成为一个巨大的数字列表
It looks like you might be looking for LINQ .SelectMany
method. 看起来您可能正在寻找LINQ
.SelectMany
方法。 A quote from MSDN: 来自MSDN的报价:
Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence
将序列的每个元素投影到IEnumerable并将结果序列展平为一个序列
List<int> list2 = list.SelectMany(l => l).ToList();
If you want numbers to be ordered in a specific order you could use .OrderBy
before executing the query (which will be executed when we call .ToList
) 如果希望按特定顺序对数字进行排序,则可以在执行查询之前使用
.OrderBy
(将在我们调用.ToList
时执行)。
List<int> list2 = list.SelectMany(l => l).OrderBy(i => i).ToList();
关于什么?
var list2 = a.Concat(b).ToList();
I would use the aggregate function. 我会使用聚合函数。 It is better when you have bigger collection.
拥有更多收藏时会更好。
List<int> list2 = list.Aggregate(new List<int>(), (ag, l) => { ag.AddRange(l); return ag; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.