我有一个集合单维的是这样的:
I have a collection uni-dimensional like this:
[1,2,4,5.....n]
我想该集合转换成一个二维的收藏是这样的:
I would like to convert that collection in a bi-dimensional collection like this:
[[1,2,3],
[4,5,6],
...]
基本上我想组或分割,如果你想,在N成员组数组
Basically I want to group or split if you want, the array in groups of 'n' members
我可以用的foreach
语句做到这一点,但我目前正在学习LINQ等等,而不是通过所有元素进行迭代,并创建一个新的数组手动我想用LINQ功能(如适用)
I can do it with a foreach
statement, but I am currently learning LINQ so instead of iterating through all elements and create a new array manually I would like to use the LINQ features (if applicable)
有没有LINQ的功能,以帮助我做到这一点?
Is there any LINQ function to help me to accomplish this??
我想在 GROUPBY
或的SelectMany
我不知道他们是否会帮助我,虽然,但他们可能
I was thinking in the GroupBy
or SelectMany
I do not know if they will help me though but they might
任何帮助将是真正的AP preciate它=):**
Any help will be truly appreciate it =) :**
您可以按索引的批量大小之分,像这样的:
You can group by the index divided by the batch size, like this:
var batchSize = 3;
var batched = orig
.Select((Value, Index) => new {Value, Index})
.GroupBy(p => p.Index/batchSize)
.Select(g => g.Select(p => p.Value).ToList());