假设我有
var input = new int[] { 0, 1, 2, 3, 4, 5 };
如何将它们分组成对?
var output = new int[][] { new int[] { 0, 1 }, new int[] { 2, 3 }, new int[] { 4, 5 } };
最好使用 LINQ
最佳答案
input
.Select((value, index) => new { PairNum = index / 2, value })
.GroupBy(pair => pair.PairNum)
.Select(grp => grp.Select(g => g.value).ToArray())
.ToArray()
关于c# - 如何按索引对项目进行分组? C# 联机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1290451/