Skip to main content

How to Get the First Few Items with LINQ Take in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Take(3) gives you the first three items of a sequence. Ask for more than there are and you simply get all of them, with no error.

Microsoft's reference: Returns a specified number of contiguous elements from the start of a sequence. It always counts from the start, and it never fails because the list is too short.

Syntax​

Syntax
IEnumerable<T> <first> = <items>.Take(<count>);
PartWhat it is
<items>Any sequence
<count>How many items to take from the start
<first>Up to <count> items, in order

The reference covers both edges: If count exceeds the number of elements in source, all elements of source are returned, and If count is less than or equal to zero, source is not enumerated, which gives you an empty sequence.

The Example​

A console app called LinqTake takes 3, 10 and 0 items from a list of five numbers.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n LinqTake
cd LinqTake
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/LinqTake/LinqTake.csproj:
Determining projects to restore...
Restored /Users/you/source/LinqTake/LinqTake.csproj (in 48 ms).
Restore succeeded.

dotnet new console -n LinqTake creates a folder named LinqTake with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or LinqTake.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the numbers:

Program.cs (the numbers)
int[] numbers = [10, 20, 30, 40, 50];

Then the three takes:

Program.cs (the takes)
Console.WriteLine(string.Join(", ", numbers.Take(3)));
Console.WriteLine(string.Join(", ", numbers.Take(10)));
Console.WriteLine($"[{string.Join(", ", numbers.Take(0))}]");

The last line prints between brackets, so you can see an empty result.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
10, 20, 30
10, 20, 30, 40, 50
[]

macOS-style terminal window titled Terminal — LinqTake showing dotnet run: 10, 20, 30, then 10, 20, 30, 40, 50, then empty brackets

Take(3) gave the first three. Take(10) asked for more than the five there are and got all five. Take(0) gave nothing.

The Complete File​

macOS-style code window titled Program.cs — LinqTake showing an array of five numbers and three Console.WriteLine calls that join Take 3, Take 10 and Take 0

Program.cs
int[] numbers = [10, 20, 30, 40, 50];

Console.WriteLine(string.Join(", ", numbers.Take(3)));
Console.WriteLine(string.Join(", ", numbers.Take(10)));
Console.WriteLine($"[{string.Join(", ", numbers.Take(0))}]");

Common Mistakes​

MistakeWhat happensDo this instead
Using Take for the last itemsIt always counts from the startUse TakeLast: numbers.TakeLast(2) returned 40, 50
Expecting an error for a negative countTake(-1) returns an empty sequence: it printed []Check the count yourself if a negative value means a bug

FAQ​

How do I get the second page of a list?​

Skip the first page, then Take one page: numbers.Skip(2).Take(2) returned 30, 40.

Can Take use a range?​

Yes: numbers.Take(1..3) returned 20, 30, the items at positions 1 and 2.

Conclusion​

items.Take(n) returns the first n items, or all of them when there are fewer, and nothing for 0 or less. Pair it with Skip for pages and use TakeLast for the end.

Additional Resources​