I would like to split a list in parts, without knowing how much items I will have in that list. The question is different from those of you who wants to split a list into chunk of fixed size.
int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
I would like the values to be splitted vertically.
Splitted in 2 :
-------------------
| item 1 | item 6 |
| item 2 | item 7 |
| item 3 | item 8 |
| item 4 | item 9 |
| item 5 | |
Splitted in 3:
| item 1 | item 4 | item 7 |
| item 2 | item 5 | item 8 |
| item 3 | item 6 | item 9 |
Splitted in 4:
| item 1 | item 4 | item 6 | item 8 |
| item 2 | item 5 | item 7 | item 9 |
| item 3 | | | |
I've found a few c# extensions that can do that but it doesn't distribute the value the way I want. Here's what I've found:
// this technic is an horizontal distribution
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
int i = 0;
var splits = from item in list
group item by i++ % parts into part
select part.AsEnumerable();
return splits;
}
the result is this but my problem is that the value are distributed horizontally:
| item 1 | item 2 |
| item 3 | item 4 |
| item 5 | item 6 |
| item 7 | item 8 |
| item 9 | |
or
| item 1 | item 2 | item 3 |
| item 4 | item 5 | item 6 |
| item 7 | item 8 | item 9 |
any idea how I can distribute my values vertically and have the possibility to choose the number of parts that i want?
In real life
For those of you who want to know in which situation I would like to split a list vertically, here's a screenshot of a section of my website: