LINQ TakeWhile Partition Operator

  Total Views : 262
  Comments : 0
 
  Keywords : How to use linq takewhile operator in c# / vb.net with example, Linq takewhile partition operator to get elements from list based on condition defined with example in c# / vb.net

Here we will learn linq takewhile partition operator with example in c#, vb.net and how to use linq takewhile partition operator to get elements from list / collection based on the condition specified in query syntax and method syntax with examples.

LINQ TakeWhile Partition Operator

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

Syntax of LINQ TakeWhile Operator

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

 

C# Code

 

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

VB.NET Code

 

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

If you observe above syntax we are getting elements from the list where element starts with “U

Example of LINQ TakeWhile in Method Syntax

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 above example we used TakeWhile () Operator and a lambda expression in which we specified that it selects only the countries that starts with “U”. So it will return elements till the condition holds true for the items in 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 that have starting letter “U”. So it returns only the first two elements.

Output of LINQ TakeWhile Operator Example

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

 

US

UK

Example of LINQ TakeWhile in Query Syntax

Following is the example of using LINQ TakeWhile operator in query syntax to get the elements from 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

Ouput of LINQ TakeWhile() Operator 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 sequence in list / collection based on the condition we defined in expression.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User