C#idiomatic C#
partitioning-datacsharp/linq/standard-query-operators

LINQ

Partitioning data

Learn how to partition data in LINQ. View an illustration showing the results of partitioning operations.

  • page csharp/linq/standard-query-operators/partitioning-data
  • updated 05/29/2024

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.


author: BillWagner ms.author: wiwagn ms.topic: include ms.date: 05/29/2024

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. System.Linq.Enumerable.Skip
System.Linq.Queryable.Skip
SkipWhile Skips elements based on a predicate function until an element doesn't satisfy the condition. Not applicable. System.Linq.Enumerable.SkipWhile
System.Linq.Queryable.SkipWhile
Take Takes elements up to a specified position in a sequence. Not applicable. System.Linq.Enumerable.Take
System.Linq.Queryable.Take
TakeWhile Takes elements based on a predicate function until an element doesn't satisfy the condition. Not applicable. System.Linq.Enumerable.TakeWhile
System.Linq.Queryable.TakeWhile
Chunk Splits the elements of a sequence into chunks of a specified maximum size. Not applicable. System.Linq.Enumerable.Chunk
System.Linq.Queryable.Chunk

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


author: '' ms.author: '' ms.topic: include ms.date: 08/11/2025

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

sampledocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/PartitionExamples.csTake

csharp

namespace StandardQueryOperators;
public class PartitionExamples
{
    public static void RunAllSnippets()
    {
        Console.WriteLine("Take");
        TakeExample();
        Console.WriteLine("Skip");
        SkipExample();
        Console.WriteLine("TakeWhile");
        TakeWhileExample();
        Console.WriteLine("SkipWhile");
        SkipWhileExample();
        Console.WriteLine("Chuck");
        ChuckExample();
    }

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

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


    private static void TakeWhileExample()
    {
        // <TakeWhile>
        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
        // </TakeWhile>
    }


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

private static void ChuckExample()
    {
        // <Chunk>
        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
        // </Chunk>
    }
}

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

sampledocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/PartitionExamples.csSkip

csharp

namespace StandardQueryOperators;
public class PartitionExamples
{
    public static void RunAllSnippets()
    {
        Console.WriteLine("Take");
        TakeExample();
        Console.WriteLine("Skip");
        SkipExample();
        Console.WriteLine("TakeWhile");
        TakeWhileExample();
        Console.WriteLine("SkipWhile");
        SkipWhileExample();
        Console.WriteLine("Chuck");
        ChuckExample();
    }

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

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


    private static void TakeWhileExample()
    {
        // <TakeWhile>
        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
        // </TakeWhile>
    }


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

private static void ChuckExample()
    {
        // <Chunk>
        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
        // </Chunk>
    }
}

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.

sampledocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/PartitionExamples.csTakeWhile

csharp

namespace StandardQueryOperators;
public class PartitionExamples
{
    public static void RunAllSnippets()
    {
        Console.WriteLine("Take");
        TakeExample();
        Console.WriteLine("Skip");
        SkipExample();
        Console.WriteLine("TakeWhile");
        TakeWhileExample();
        Console.WriteLine("SkipWhile");
        SkipWhileExample();
        Console.WriteLine("Chuck");
        ChuckExample();
    }

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

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


    private static void TakeWhileExample()
    {
        // <TakeWhile>
        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
        // </TakeWhile>
    }


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

private static void ChuckExample()
    {
        // <Chunk>
        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
        // </Chunk>
    }
}

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.

sampledocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/PartitionExamples.csSkipWhile

csharp

namespace StandardQueryOperators;
public class PartitionExamples
{
    public static void RunAllSnippets()
    {
        Console.WriteLine("Take");
        TakeExample();
        Console.WriteLine("Skip");
        SkipExample();
        Console.WriteLine("TakeWhile");
        TakeWhileExample();
        Console.WriteLine("SkipWhile");
        SkipWhileExample();
        Console.WriteLine("Chuck");
        ChuckExample();
    }

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

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


    private static void TakeWhileExample()
    {
        // <TakeWhile>
        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
        // </TakeWhile>
    }


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

private static void ChuckExample()
    {
        // <Chunk>
        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
        // </Chunk>
    }
}

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

sampledocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/PartitionExamples.csChunk

csharp

namespace StandardQueryOperators;
public class PartitionExamples
{
    public static void RunAllSnippets()
    {
        Console.WriteLine("Take");
        TakeExample();
        Console.WriteLine("Skip");
        SkipExample();
        Console.WriteLine("TakeWhile");
        TakeWhileExample();
        Console.WriteLine("SkipWhile");
        SkipWhileExample();
        Console.WriteLine("Chuck");
        ChuckExample();
    }

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

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


    private static void TakeWhileExample()
    {
        // <TakeWhile>
        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
        // </TakeWhile>
    }


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

private static void ChuckExample()
    {
        // <Chunk>
        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
        // </Chunk>
    }
}

The preceding C# code:

See also