LINQ Skip Operator in VB.NET

This article defines the basic use of Skip operator in LinQ.
  • 2191

This article defines the basic use of Skip operator in LinQ.

Skip Operator

The Skip operator skips a given number of elements from a sequence and then yields the remainder of the sequence. or Skip discards the first n elements and emits the rest.

Understanding Skip

The below defines the names can be an array of numbers, strings,
or even objects. Here is an example of name.

Dim names As String() = {"Ram", "Hari", "Bharat", "Ravi", "Rahul", "Monu", "Deepak", "Munesh", "Naresh", "Raju"}

Here is a simple Skip operator. 
Dim query As IEnumerable(Of String) = names.Skip(3)
Example1
This example defines the array of strings. If we want to Skip first three 
name from array of string and emits the rest.

Module Module1

    Sub Main()

        Dim names As String() = {"Ram", "Hari", "Bharat", "Ravi", "Rahul", "Monu", "Deepak", "Munesh", "Naresh", "Raju"}

        Dim query As IEnumerable(Of String) = names.Skip(3)

        For Each s As String In query

            Console.WriteLine(s)

        Next

    End Sub

End Module

OUTPUT

s1.gif
Example 2
This example defines all the All elements starting from first element divisible by 4.

Module Module1

    Sub Main()

        Dim numbers() As Integer = {5, 7, 1, 4, 9, 8, 6, 7, 2, 0}

        Dim number = From num In numbers

        Skip While num Mod 4 <> 0

        Console.WriteLine("All elements starting from first element divisible by 4:")

        For Each n In number

            Console.WriteLine(n)

        Next

    End Sub

End Module

OUTPUT

s2.gif

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.