LINQ Interview Question #131

How do Chunk and LINQ partitioning differ from Skip and Take pagination?

Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced

Quick Interview Answer

Chunk divides an IEnumerable<T> sequence into consecutive arrays of a specified maximum size. Skip and Take select a particular range and are commonly used to compose paging queries.

Detailed Explanation

Chunk divides an IEnumerable<T> sequence into consecutive arrays of a specified maximum size. Skip and Take select a particular range and are commonly used to compose paging queries.

Chunk is useful for processing in-memory data in batches. For database paging, Skip/Take or keyset pagination is generally more appropriate because IQueryable providers can translate those operations.

Code Example

foreach (int[] batch in numbers.Chunk(100))
{
    ProcessBatch(batch);
}

var page = query
    .Skip(40)
    .Take(20);