Partitioning data (C#)

Partitioning in LINQ refers to the operation of dividing an input sequence into two sections, without rearranging the elements, and then returning one of the sections.

Important

These samples use an System.Collections.Generic.IEnumerable<T> data source. Data sources based on System.Linq.IQueryProvider use System.Linq.IQueryable<T> data sources and expression trees. Expression trees have limitations on the allowed C# syntax. Furthermore, each IQueryProvider data source, such as EF Core may impose more restrictions. Check the documentation for your data source.

The following illustration shows the results of three different partitioning operations on a sequence of characters. The first operation returns the first three elements in the sequence. The second operation skips the first three elements and returns the remaining elements. The third operation skips the first two elements in the sequence and returns the next three elements.

Illustration that shows three LINQ partitioning operations.

The standard query operator methods that partition sequences are listed in the following section.

Operators

Method names Description C# query expression syntax More information
Skip Skips elements up to a specified position in a sequence. Not applicable. Enumerable.Skip
Queryable.Skip
SkipWhile Skips elements based on a predicate function until an element doesn't satisfy the condition. Not applicable. Enumerable.SkipWhile
Queryable.SkipWhile
Take Takes elements up to a specified position in a sequence. Not applicable. Enumerable.Take
Queryable.Take
TakeWhile Takes elements based on a predicate function until an element doesn't satisfy the condition. Not applicable. Enumerable.TakeWhile
Queryable.TakeWhile
Chunk Splits the elements of a sequence into chunks of a specified maximum size. Not applicable. Enumerable.Chunk
Queryable.Chunk

All the following examples use Enumerable.Range(Int32, Int32) to generate a sequence of numbers from 0 through 7.

You use the Take method to take only the first elements in a sequence:

foreach (int number in Enumerable.Range(0, 8).Take(3))
{
    Console.WriteLine(number);
}
// This code produces the following output:
// 0
// 1
// 2

You use the Skip method to skip the first elements in a sequence, and use the remaining elements:

foreach (int number in Enumerable.Range(0, 8).Skip(3))
{
    Console.WriteLine(number);
}
// This code produces the following output:
// 3
// 4
// 5
// 6
// 7

The TakeWhile and SkipWhile methods also take and skip elements in a sequence. However, instead of a set number of elements, these methods skip or take elements based on a condition. TakeWhile takes the elements of a sequence until an element doesn't match the condition.

foreach (int number in Enumerable.Range(0, 8).TakeWhile(n => n < 5))
{
    Console.WriteLine(number);
}
// This code produces the following output:
// 0
// 1
// 2
// 3
// 4

SkipWhile skips the first elements, as long as the condition is true. The first element not matching the condition, and all subsequent elements, are returned.

foreach (int number in Enumerable.Range(0, 8).SkipWhile(n => n < 5))
{
    Console.WriteLine(number);
}
// This code produces the following output:
// 5
// 6
// 7

The Chunk operator is used to split elements of a sequence based on a given size.

int chunkNumber = 1;
foreach (int[] chunk in Enumerable.Range(0, 8).Chunk(3))
{
    Console.WriteLine($"Chunk {chunkNumber++}:");
    foreach (int item in chunk)
    {
        Console.WriteLine($"    {item}");
    }

    Console.WriteLine();
}
// This code produces the following output:
// Chunk 1:
//    0
//    1
//    2
//
//Chunk 2:
//    3
//    4
//    5
//
//Chunk 3:
//    6
//    7

The preceding C# code:

  • Relies on Enumerable.Range(Int32, Int32) to generate a sequence of numbers.
  • Applies the Chunk operator, splitting the sequence into chunks with a max size of three.

See also