About 3 years ago, I posted an article about Too much to digest? Slice it up in chunks!, which worked, but was a rather complex way of slicing items into smaller bites.


For a while now, there has been an alternative; the PartitionCollection<T> class in the Cuemon.Collections.Generic namespace. Depending on your .NET installation, there are also extension methods ToPartitionCollection that might come in handy.

The usage is simple, as illustrated here (i have kept the example as close to the original as possible):

using System.Diagnostics;  
using System.Linq;  
using Cuemon.Collections.Generic;  
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cuemon.Examples  
    public class PartitionCollectionTest
        public void ChunkTest()
            var workload = 4096;
            var partitionSize = 255;
            var simulatedWorkload = Enumerable.Range(0, workload).ToPartitionCollection(partitionSize);

            Trace.WriteLine("Work to process: {0} items.".FormatWith(workload));
            Trace.WriteLine("Partitions for work: {0} runs.".FormatWith(simulatedWorkload.PartitionCount()));

            while (simulatedWorkload.HasPartitions)
                var chunkedWorkload = simulatedWorkload.ToList();
                foreach (var work in chunkedWorkload)
                    // process your work here
                Trace.WriteLine("Remaining work to process: {0} items.".FormatWith(simulatedWorkload.Remaining));
The result is well familiar:
Work to process: 4096 items.  
Partitions for work: 17 runs.  
Remaining work to process: 3841 items.  
Remaining work to process: 3586 items.  
Remaining work to process: 3331 items.  
Remaining work to process: 3076 items.  
Remaining work to process: 2821 items.  
Remaining work to process: 2566 items.  
Remaining work to process: 2311 items.  
Remaining work to process: 2056 items.  
Remaining work to process: 1801 items.  
Remaining work to process: 1546 items.  
Remaining work to process: 1291 items.  
Remaining work to process: 1036 items.  
Remaining work to process: 781 items.  
Remaining work to process: 526 items.  
Remaining work to process: 271 items.  
Remaining work to process: 16 items.  
Remaining work to process: 0 items.  

As always; I hope you found these small articles inspiring - and happy coding!