Chunks

Chunks are a way to split a collection into smaller groups or "chunks" of a specific size. This way you can break a long list of items into smaller groups to make it easier to work with.


Our Products list has 12 items. We can break it up into groups of 3 items and handle each chunk in a foreach:

IEnumerable<Product[]> chunks = ProductList.Products.Chunk(3);

foreach (Product[] chunk in chunks)
{
    foreach (Product product in chunk)
    {
        Console.WriteLine(product.Title);
    }
}


Benefits of chunks:

  1. Processing manageable parts is easier than dealing with a huge list.
  2. It manages memory more efficiently.
  3. Each chunk could be processed in parallel, which is a great improvement.
  4. It improves error handling. Errors in a certain chunk don't affect the other chunks. You can handle errors in a chunk, rather than on the whole list.
  5. Testing is made more efficient since you test a specific portion of the dataset.