LINQ Skip Operator

  Total Views : 269
  Comments : 0
 
  Keywords : How to use linq skip operator to skip elements in list / collection and get remaining elements in c#, vb.net example, Linq skip operator to skip elements in list in c# / vb.net with example

Here we will learn linq skip partition operator with example in c#, vb.net and how to use linq skip operator to skip some elements in list / collection and return remaining elements from collection with example in query syntax and method syntax using c#, vb.net.

LINQ Skip Partition Operator

In LINQ skip operator is used to skips the specified number of elements from the list / collection and returns the remaining elements from collection.

Syntax of LINQ Skip Operator

Following is the syntax of using LINQ skip operator to skip specified number of elements from the collection and return the remaining elements from collection.

 

C# Code

 

IEnumerable<string> result = countries.Skip(3);

VB.NET Code

 

Dim result As IEnumerable(Of String) = countries.Skip(3)

If you observe above syntax we are skipping first three elements by using “Skip(3)” and getting remaining elements from the collection.

Example of LINQ Skip Operator in Method Syntax

Following is the example of using LINQ skip operator in method syntax to skip specified elements from collection and get remaining elements from collection.

 

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.Skip(3);

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.Skip(3)

For Each s As String In result

Console.WriteLine(s)

Next

Console.ReadLine()

End Sub

End Module

If you observe above example, we are trying to skip the first three countries. So we passed “3” as an input parameter to the SKIP Operator. It will display the rest of the countries.

Output of LINQ Skip Operator in Method Syntax Example

Following is the result of using skip operator in LINQ to skip some elements in list and get remaining elements in method syntax.

 

Russia

China

ustralia

Argentina

Example of LINQ Skip Operator in Query Syntax

Following is the example of using LINQ skip operator in query syntax to skip specified number of elements from collection.

 

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).Skip(3);

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).Skip(3)

For Each s As String In result

Console.WriteLine(s)

Next

Console.ReadLine()

End Sub

End Module

Ouput of LINQ Skip() Operator in Query Syntax

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

 

Russia

China

Australia

Argentina

This is how we can use LINQ skip method / operator in method syntax and query syntax to skip some elements in list / collection and return remaining elements in sequence.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User