LINQ TakeWhile Partition Operator

In LINQ, the TakeWhile operator is used to get the elements from the list/collection data source as long as the specified condition holds true in the expression. When the condition stops holding true, it does not return those elements.

Syntax of LINQ TakeWhile Operator

Following is the syntax of using the LINQ TakeWhile operator to get the elements from a list based on the condition specified.

LINQ TakeWhile Syntax in C#

IEnumerable<string> result = countries.TakeWhile(x => x.StartsWith("U"));

LINQ TakeWhile Syntax in VB.NET

Dim result As IEnumerable(Of String) = countries.TakeWhile(Function(x) x.StartsWith("U"))

If you observe the above syntax, we get elements from the list where the element starts with “U”.

LINQ TakeWhile in Method Syntax Example

Following is the example of using LINQ TakeWhile in method syntax to get elements from list/collection based on condition.

 

C# Code

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
       string[] countries = { "US", "UK", "Russia", "China", "Australia", "Argentina" };
       IEnumerable<string> result = countries.TakeWhile(x => x.StartsWith("U"));
       foreach (string s in result)
       {
         Console.WriteLine(s);
       }
       Console.ReadLine();
    }
  }
}

 VB.NET Code

 

Module Module1
Sub Main()
Dim countries As String() = {"US", "UK", "Russia", "China", "Australia", "Argentina"}
Dim result As IEnumerable(Of String) = countries.TakeWhile(Function(x) x.StartsWith("U"))
For Each s As String In result
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module

In the above example, we used TakeWhile () Operator and a lambda expression. We specified that it selects only the countries that start with “U”. It will return elements until the condition holds true for the items in the list/collection.

 

When the condition is no longer true, it does not take the element. So in the array, we have only the first two countries with the starting letter “U”. It returns only the first two elements.

Result of LINQ TakeWhile Operator Example

Following is the result of LINQ TakeWhile in method syntax example to get the elements from a list based on condition.

 

US
UK

LINQ TakeWhile in Query Syntax Example

Following is the example of using the LINQ TakeWhile operator in query syntax to get the elements from a list based on condition.

 

C# Code

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      string[] countries = { "US", "UK", "Russia", "China", "Australia", "Argentina" };
      IEnumerable<string> result = (from x in countries select x).TakeWhile(x => x.StartsWith("U"));
      foreach (string s in result)
      {
        Console.WriteLine(s);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1
Sub Main()
Dim countries As String() = {"US", "UK", "Russia", "China", "Australia", "Argentina"}
Dim result As IEnumerable(Of String) = (From x In countries).TakeWhile(Function(x) x.StartsWith("U"))
For Each s As String In result
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module

Result of LINQ TakeWhile() in Query Syntax

If we execute the above program, we will get output like as shown below

 

US
UK

This is how we can use LINQ TakeWhile partition operator in method syntax/query syntax to get elements from the sequence in list/collection based on the condition we defined in expression.