TakeWhile and SkipWhile Operator in Linq using VB.NET

Here we will see how to use TakeWhile and SkipWhile Operator in Linq.
  • 3311

TakeWhile operator

The TakeWhile operator yields elements from a sequence while a test is true and then skips the remainder of the sequence. The TakeWhile operator allocates and returns an enumerable object that captures the arguments passed to the operator.

For example

The below example defines the TakeWhile operator.

Module Module1

    Sub Main()

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

        Dim takeWhileSmall = numbers.TakeWhile(Function(n) n < 8)

        For Each s As Integer In takeWhileSmall

            Console.WriteLine(s)

        Next

    End Sub

End Module

OUTPUT

tw.gif
 

The above output defines that if the test condition is false after 3,5,2,7,4,1 element.

SkipWhile Operator

SkipWhile enumerates the input sequence, ignoring each item until the given predicate is false or, The SkipWhile operator skips elements from a sequence while a test is true and then yields the remainder of the sequence.

For example

Module Module1

    Sub Main()

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

        Dim skipWhileSmall = numbers.SkipWhile(Function(n) n < 8)

        For Each s As Integer In skipWhileSmall

            Console.WriteLine(s)

        Next

    End Sub

End Module

OUTPUT

sw.gif

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.