LINQ SkipWhile Partition Operator

  Total Views : 201
  Comments : 0
 
  Keywords : Linq skipwhile method with example in c# / vb.net, How to use Linq skipwhile method to skip some elements in list / collection based on defined condition withe example in c#, vb.net

Here we will learn linq skipwhile partition operator with example in c#, vb.net and how to use linq skipwhile operator to skip elements in sequence based on condition defined and return remaining elements in query syntax and method syntax with examples in c#, vb.net. 

LINQ SkipWhile Partition Operator

In LINQ SkipWhile operator is used to skips the elements in sequence till the specified condition holds true then it returns remaining elements from the list / collection.

Syntax of LINQ SkipWhile Operator

Following is the syntax of using LINQ SkipWhile() operator to skip elements in list which satisfies defined conditions and returns remaining elements from collection.

 

C# Code

 

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

VB.NET Code

 

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

If you observe above syntax we are skipping elements from list which starts with “U” and showing remaining elements.

Example of LINQ SkipWhile Operator in Method Syntax

Following is the example of using LINQ SkipWhile operator in method syntax to get elements from list which will not satisfy specified condition.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace LINQExamples

{

classProgram

{

staticvoid Main(string[] args)

{

string[] countries = { "US", "UK","India", "Russia", "China", "Australia", "Argentina" };

IEnumerable<string> result = countries.SkipWhile(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", "India", "Russia", "China", "Australia", "Argentina"}

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

ForEach s As String In result

Console.WriteLine(s)

Next

Console.ReadLine()

EndSub

End Module

In the above example we have an array string of countries. We want to display the countries whose name does not start with “U”. So it will start from the first country in the array and will skip those countries whose first letter is “U” and will return the rest of the countries.

Output of LINQ SkipWhile Operator Example

Following is the result of LINQ SkipWhile operator example in method syntax to skip elements from list based on specified condition.

 

India

Russia

China

Australia

Argentina

Example of LINQ SkipWhile in Query Syntax

Following is the example of using LINQ SkipWhile operator in query syntax.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace LINQExamples

{

classProgram

{

staticvoid Main(string[] args)

{

string[] countries = { "US", "UK","India", "Russia", "China", "Australia", "Argentina" };

IEnumerable<string> result = (from x in countries select x).SkipWhile(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", "India", "Russia", "China", "Australia", "Argentina"}

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

ForEach s As String In result

Console.WriteLine(s)

Next

Console.ReadLine()

EndSub

End Module

Output of LINQ SkipWhile in Query Syntax Example

Following is the result of LINQ SkipWhile operator in query syntax example.

 

India

Russia

China

Australia

Argentina

This is how we can use LINQ skipwhile partition operator in query syntax and methof syntax to skip some elements in list based on specified condition and return remaining elements from list / collection.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User