thatuxguy thatuxguy - 1 month ago 10x
C# Question

Best way to split an array

I need to find out what the best way to split an array into smaller "chunks" would be.

I am passing over about 1200 items, and need to split these into easier to handle groups of 100, then i need to pass them over to processed.

Could any one please make some suggestions?


You can use LINQ to group all items by the chunk size and create new Arrays afterwards.

// build sample data with 1200 Strings
string[] items = Enumerable.Range(1, 1200).Select(i => "Item" + i).ToArray();
// split on groups with each 100 items
String[][] chunks = items
                    .Select((s, i) => new { Value = s, Index = i })
                    .GroupBy(x => x.Index / 100)
                    .Select(grp => grp.Select(x => x.Value).ToArray())

for (int i = 0; i < chunks.Length; i++)
    foreach (var item in chunks[i])
        Console.WriteLine("chunk:{0} {1}", i, item);

Note that it's not necessary to create new arrays(needs cpu cycles and memory). You could also use the IEnumerable<IEnumerable<String>> when you omit the two ToArrays.

Here's the running code: