👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ Chunk to Split Data

Using LINQ Chunk to Split Data

Author - Abdul Rahman (Bhai)

LINQ

26 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

In this article, let's learn about how to use Chunk in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Using LINQ Distinct to Select Specific Data in Collections.

Why we gonna do?

Splitting a large collection into fixed-size batches is a common need, especially for pagination, bulk API calls, or batch processing. Without Chunk(), you'd track an index and manually slice the list. Chunk() expresses this intent clearly and concisely with minimal code.

How we gonna do?

Split collections into smaller collections using Chunk

Let's partition data into other format. We can split our (Larger) Collection into array of smaller collections using Chunk() method. This will give list of data array. What Chunk(n) does is that it takes first n items from collection and build an array with that items and put them into first item of our final list. Then it takes second n items from collection build an array and put it into second item of our final list forming collection of collections.


List<Product> products = GetProducts();

//Method Syntax
List<Product[]> partitionedProducts = products
                                      .Chunk(1)
                                      .ToList();
                    
//Query Syntax
List<Product[]> partitionedProducts = (from product in products select product)
                                      .Chunk(1)
                                      .ToList();
            
Demo Space

Summary

In this article we learn't how to split data within collection using Chunk. This can be used for pagination for small in-memory collections to improve user experience. All these can be used with any IEnumerable or IQueryable types.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • Chunk
  • Split