Here's the next topic in our ongoing Bite-Sized .NET 6 series: the ability to split collections into groups of smaller collections using LINQ's Chunk() method!

Current Implementation

In the prior versions of .NET, there wasn't a native way to break a collection into a set of smaller collections.

int[] numbers = new int[] {6, 5, 1, 9, 18, 5, 3, 21};

//Now what?

You could use the LINQ Skip() and Take() methods, but they weren't very nice to use.

var coll1 = numbers.Take(2);
var coll2 = numbers.Skip(2).Take(2);
var coll3 = numbers.Skip(4).Take(2);
var coll4 = numbers.Skip(6).Take(2);

A bit of cleverness would get you a better solution to break apart a collection, such as this highly-voted answer from StackOverflow:

static class LinqExtensions
{
    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;
    }
}

But there was no native way to do this in .NET, until now.

New Implementation

.NET 6 includes a new method in the LINQ library, Chunk(), which breaks collections into a set of smaller collections.

Say we have a large collection of random numbers...

List<int> numbers = new();

int counter = 0;
Random rand = new(DateTime.Now.Millisecond);
while(counter < 100)
{
    numbers.Add(rand.Next(1, 1000));
    counter++;
}

We want to subdivide this collection of 100 numbers into 10 sets of 10 numbers. We can do this using the new Chunk() method, which takes as a parameter the size of the sublists that will be created.

//Child collections will be comprised of 10 elements each.
IEnumerable<int[]> sublists = numbers.Chunk(10); 

Because our original collection was comprised of 100 elements, sublists will be comprised of ten collections, each of which have ten elements.

Other Considerations

But what happens if the size of the sublists doesn't divide evenly into the number of elements in the original list?

Let's use that collection of 100 random integers again, but this time, chunk it into collections of size 8.

IEnumerable<int[]> sublists = numbers.Chunk(8);

How many collections does this result in, and how many elements does each collection have?

It turns out, this results in 13 collections. The first 12 of these have 8 elements, since 8 goes into 100 evenly 12 times. The final collection only has 4 elements, since 100 % 8 is 4.

Be aware of this behavior if you need to chunk collections of odd sizes.

Demo Project

As with all posts in this series, there is a demo project you can check out over on GitHub that shows examples of LINQ's Chunk() method and its behavior. Check it out!

GitHub - exceptionnotfound/NET6BiteSizeDemos
Contribute to exceptionnotfound/NET6BiteSizeDemos development by creating an account on GitHub.

Happy Coding!

EDITOR'S NOTE: This post was originally supposed to come out on Monday, 11th Oct, 2021, but I was out of town and forgot to schedule it. Posts in this series will resume Monday, 18th Oct, 2021.