+92 332 4229 857 99ProjectIdeas@Gmail.com

LINQ TakeWhile() Method (Vb.net)



The following code shows you how to apply some condition on array and taking those elements. For example array contains {2,3,4,7,8} and you want all those elements which are odd then applying LINQ TakeWhile() method you can do this although there is another way. But through this see the code below.





Dim numbers() As Integer = {5, 3, 9, 8, 10}

Dim query As IEnumerable(Of Integer) = _
    numbers.TakeWhile(Function(x) x Mod 2 <> 0)


      For Each i In query
          Console.Write(i.ToString() & " ")
      Next

Output of the program is:
5 3 9 (because these three are odd)

Another Example: 

Dim numbers() As Integer = {59, 82, 70, 56, 92, 98, 85}

Dim query As IEnumerable(Of Integer) = _
    numbers.TakeWhile(Function(x) _
                              String.Compare("56", x.ToString(), True) <> 0)


    For Each i In query
         Console.Write(i.ToString() & " ")
    Next

Output of the program is:
59 82 70 (if finds 56 print all previous elements)



0 comments: