Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ Take(), Skip() and Chunk() methods

Reference for LINQ Take, Skip, TakeWhile, SkipWhile, TakeLast, SkipLast and Chunk in C#: paging, ranges, edge cases and performance.

Take(n) returns the first n elements and Skip(n) returns everything after the first n. Used together they implement paging: Skip(page * size).Take(size). The While variants stop or start at the first element that fails a condition, and TakeLast() and SkipLast() work from the end.

Chunk(size) (.NET 6+) splits a sequence into arrays of at most size elements. All of these are deferred except that TakeLast(), SkipLast() and Chunk() have to buffer part of the source.

C#
var page = items.Skip(20).Take(10);
var lastThree = items.TakeLast(3);
foreach (int[] batch in numbers.Chunk(100)) { }

Try it

C# Paging and while-variants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Chunk and batching

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static IEnumerable<T> Take<T>(this IEnumerable<T> source, int count);
public static IEnumerable<T> Take<T>(this IEnumerable<T> source, Range range);
public static IEnumerable<T> Skip<T>(this IEnumerable<T> source, int count);
public static IEnumerable<T> TakeWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate);
public static IEnumerable<T> SkipWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate);
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int count);
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source, int count);
public static IEnumerable<T[]> Chunk<T>(this IEnumerable<T> source, int size);
Name Description
count How many elements to take or skip. Zero or negative takes none or skips none.
range A Range such as 2..5 or ^3.., selecting by position.
predicate A condition, optionally with the element index as a second parameter.
size The maximum chunk length; must be greater than 0.

Returns: a lazy sequence (for Chunk(), a sequence of arrays; the last array may be shorter).

Errors: ArgumentNullException for a null source or predicate; ArgumentOutOfRangeException from Chunk() when size is less than 1 (thrown when you call it, not at enumeration, as the runner shows).

Usage notes

  • Order first, then page. Without a stable OrderBy() before Skip()/Take(), page contents are only as stable as the source.
  • Cost of Skip(): on a lazy sequence it still has to enumerate and discard the skipped elements, so deep pages get slower. Lists, arrays and other IList<T> sources are skipped in O(1).
  • Take() never reads more than it needs, which makes it safe on infinite generators; TakeLast() and SkipLast() must read the whole sequence (they keep a buffer of count items).
  • No exceptions for large counts. Taking more than exists returns everything; skipping more returns an empty sequence.
  • TakeWhile() stops forever at the first failure, unlike Where(), which keeps filtering. SkipWhile() stops skipping at the first failure and then yields everything, even later elements that would fail.
  • Batching: Chunk() replaces hand-written batching loops for bulk inserts or API calls with a size limit.
  • Index overload: TakeWhile((x, i) => i < 3 && x > 0) receives the position.
  • Databases: Entity Framework translates Skip()/Take() to OFFSET/LIMIT; large offsets are slow there too, so prefer keyset pagination for big tables.