LINQ SkipWhile Partition Operator

In LINQ, the SkipWhile operator is used to skip the elements in sequence until the specified condition holds true and returns the remaining elements from the list/collection.

Syntax of LINQ SkipWhile Operator

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

LINQ SkipWhile Syntax in C#

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

LINQ SkipWhile Syntax in VB.NET

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

If you observe the above syntax, we are skipping elements from the list that starts with “U” and shows the remaining elements.

LINQ SkipWhile Operator in Method Syntax Example

Following is the example of using the LINQ SkipWhile operator in method syntax to get elements from the list which will not satisfy a specified 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","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"))
For Each s As String In result
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
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.

Result of LINQ SkipWhile Operator Example

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

 

India
Russia
China
Australia
Argentina

LINQ SkipWhile in Query Syntax Example

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

 

C# Code

 

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

namespace LINQExamples
{
  class Program
  {
    static void 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"))
For Each s As String In result
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module

Result of LINQ SkipWhile in Query Syntax Example

Following is the result of the 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 method syntax to skip some elements in the list based on specified conditions and return the remaining elements from the list/collection.