In LINQ, the Skip operator is used to skip the specified number of elements from the list/collection and returns the remaining elements from a collection.
Syntax of LINQ Skip Operator
Following is the syntax of using the LINQ skip operator to skip the specified number of elements from the collection and return the remaining elements.
LINQ Skip Operator Syntax in C#
IEnumerable<string> result = countries.Skip(3);
LINQ Skip Operator Syntax in VB.NET
Dim result As IEnumerable(Of String) = countries.Skip(3)
If you observe the above syntax, we are skipping the first three elements by using “Skip(3)” and getting the remaining elements from the collection.
LINQ Skip Operator in Method Syntax Example
Following is the example of using the LINQ skip operator in method syntax to skip specified elements from collection and get the remaining elements from the 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 the 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.
Result of LINQ Skip Operator in Method Syntax Example
Following is the result of using the skip operator in LINQ to skip some elements in the list and get the remaining elements in method syntax.
Russia
China
Australia
Argentina
LINQ Skip Operator in Query Syntax Example
Following is the example of using the LINQ skip operator in query syntax to skip the specified number of elements from a 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
Result of LINQ Skip() Operator in Query Syntax
If you execute the above program, you will get the following result.
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 the remaining elements in a sequence.