Summary: in this tutorial, you’ll learn how to use the LINQ Take()
method to retrieve a specified number of elements from the beginning of a sequence.
Introduction to the LINQ Take() method
The Take()
method returns a specified number of elements from the beginning of a sequence:
IEnumerable<TSource> Take<TSource>(
this IEnumerable<TSource> source,
int count
)
Code language: C# (cs)
In this syntax:
TSource
is the type of elements of thesource
sequence.source
is the input sequencecount
is the number of elements to return.
The Take()
method returns an IEnumerable<T>
that contains the specified number of elements from the beginning of the source
sequence.
If the count
is less than or equal to zero, the Take()
method returns an empty IEnumerable<T>
object.
Like other extension methods, the Take()
method is implemented using deferred execution. It means that the query returned by the Take()
method will not execute until the elements are enumerated either by calling its GetEnumerator()
method or by using a foreach
.
If you want to get a range of elements, you can use the Range
object instead for the second argument instead of using the integer number count:
IEnumerable<TSource> Take<TSource>(
this IEnumerable<TSource> source,
Range range
)
Code language: C# (cs)
The Range
object specifies the range of elements to return. The range is defined by its starting and ending indexes, which can be specified from either the beginning or the end of the sequence.
In practice, you often use the Take()
method with the Skip()
method to skip some first elements and then take a specified number of elements.
LINQ Take() method examples
Let’s take some examples of using the LINQ Take()
method.
1) Using LINQ Take() to take some elements from a sequence
The following program demonstrates how to use the Take()
method to get the first three elements from an array of integers:
using static System.Console;
int[] numbers = { 1, 2, 3, 4, 5 };
var results = numbers.Take(3);
foreach (int number in results)
{
WriteLine(number);
}
Code language: C# (cs)
Output:
1
2
3
Code language: C# (cs)
We assigned the result of the Take()
method to the results variable and use a foreach
loop to iterate over the elements in the results sequence and write each to the console.
2) Using LINQ Take() to take some elements from a sequence using a Range object
The following program demonstrates how to use the Take()
method to retrieve a range of elements from an array:
using static System.Console;
int[] numbers = { 1, 2, 3, 4, 5 };
var results = numbers.Take(new Range(1,4));
foreach (int number in results)
{
WriteLine(number);
}
Code language: C# (cs)
Output:
2
3
4
Code language: C# (cs)
In this example, we use a Range
object that takes two arguments: the starting index and the ending index (exclusive).
The Range
object specifies a range from index 1 to index 4, which includes elements 2, 3, and 4 of the numbers array.
Using the Range
object above is equivalent to using the following Skip()
and Take()
methods:
using static System.Console;
int[] numbers = { 1, 2, 3, 4, 5 };
var results = numbers.Skip(1).Take(3);
foreach (int number in results)
{
WriteLine(number);
}
Code language: C# (cs)
Summary
- Use the LINQ
Take()
method to retrieve a specified number of elements from the beginning of a sequence or a range of elements in the sequence.