Lesson 21/31

Tutorials LINQ Mastery

Chunk: Slicing data for batch processing

On this page

Batch Processing made Easy

Before .NET 6, splitting a list into smaller batches required messy math with Skip and Take. Chunk does it in one line.

1. Why Chunk?

Many APIs and Databases have limits on payload size (e.g., 'You can only insert 500 rows at a time'). Chunk allows you to process a massive list in bite-sized pieces.


// Split 10,000 IDs into batches of 100
var batches = largeListOfIds.Chunk(100);

foreach (var batch in batches) {
    // Process 100 items...
    await SendBatchToApi(batch);
}
    

2. Memory Efficiency

Chunk is lazy-evaluated. It doesn't create all the batches in memory at once. It yields them one by one as you iterate. This allows you to process millions of items with a fixed, small memory footprint.

3. Architect Insight

Q: "What happens to the last chunk?"

Architect Answer: "If you have 105 items and you chunk by 10, you will get 10 chunks of 10, and a final chunk of 5. The last chunk is never 'padded'—it simply contains the remaining elements. This makes it safe to use for any list size without extra logic."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

LINQ Mastery
Course syllabus
General
1. Core Foundations
2. Filtering & Transformation
3. Aggregation & Quantifiers
4. Ordering & Partitioning
5. Sets & Lookups
6. Join & Grouping
7. Advanced Providers & Parallelism
8. Real-world Performance & Patterns
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details