Summary: in this tutorial, you will learn how to use the LINQ TakeWhile()
method to retrieve elements from a sequence as long as a condition is true.
Introduction to the LINQ TakeWhile() method
The TakeWhile()
method allows you to get elements from a sequence as long as a condition is true and skip the remaining elements:
IEnumerable<TSource> TakeWhile<TSource> (
this IEnumerable<TSource> source,
Func<TSource,int,bool> predicate
);
Code language: C# (cs)
In this syntax:
source
is the sequence to retrieve the elements.predicate
is a function to test each element in the source for the condition. The second parameter of thepredicate
is the index of thesource
element.
The TakeWhile()
method returns an IEnumerable
<TSource
> that contains elements from the source
sequence.
LINQ TakeWhile() method examples
Let’s take some examples of using the LINQ TakeWhile()
method.
1) Using the LINQ TakeWhile() method to filter numbers
The following example uses the LINQ TakeWhile()
method to filter numbers until the condition num < 6
is no longer true:
using static System.Console;
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.TakeWhile(num => num < 6);
foreach (var number in result)
{
WriteLine(number);
}
Code language: C# (cs)
Output:
1
2
3
4
5
Code language: C# (cs)
Note that once the TakeWhile()
encounters the number 6, the it stops and returns the filtered numbers.
2) Using the LINQ TakeWhile() method to filter strings
The following example uses the TakeWhile()
method to retrieve names from the names array until the length of the name is less than 6 characters. It stops the name Charlie
because the length of that name is 6 characters:
using static System.Console;
string[] names = { "Alice", "Bob", "Charlie", "Richard", "Eve" };
var result = names.TakeWhile(
name => name.Length < 6
);
foreach (var name in result)
{
WriteLine(name);
}
Code language: C# (cs)
Output:
Alice
Bob
Code language: C# (cs)
3) Using the LINQ TakeWhile() method to filter objects
The following example illustrates how to use the TakeWhile()
method to filter objects:
using static System.Console;
namespace LINQDemo;
public class Person
{
public required string Name
{
get; set;
}
public int Age
{
get; set;
}
}
public class Program
{
public static void Main(string[] args)
{
var people = new List<Person>
{
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 26 },
new Person { Name = "Charlie", Age = 30 },
new Person { Name = "William", Age = 22 },
new Person { Name = "Eve", Age = 18 }
};
var result = people.TakeWhile(person => person.Age < 30);
foreach (var person in result)
{
WriteLine($"Name: {person.Name}, Age: {person.Age}");
}
}
}
Code language: C# (cs)
Output:
Name: Alice, Age: 25
Name: Bob, Age: 26
Code language: C# (cs)
How it works.
First, define the Person
class that represents individuals with their names and ages
Second, use TakeWhile()
method to filter the list and retrieve persons whose age is less than 30. The TakeWhile()
method stops once it encounters a person with an age of 30 or greater.
Summary
- Use the LINQ
TakeWhile()
method to retrieve elements from a sequence as long as a condition is true and skip the remainning elements.