LINQ ThenBy and Any operator in VB.NET

This article defines the basic use of the ThenBy and any operator in LINQ.
  • 6698

This article defines the basic use of the ThenBy and any operator in LINQ.

ThenBy operator

The ThenBy operator Performs a subsequent ordering of the elements in a sequence in ascending order or, The ThenBy operator orders an ordered sequence according to one or more keys in ascending order.

The below defines the names is an array of string or even objects. Here is an example of names.

Dim names As String() = {"Rohatash", "Monu", "Ajay", "Vijay", "Ram", "Hari", "vikash", "ravi", "sarvan", "kavita"}

Using ThenBy operator

Dim query As IEnumerable(Of String) = names.OrderBy(Function(s) s.Length).ThenBy(Function(s) s)

        For Each s As [String] In query

            Console.WriteLine(s)

        Next

For example
The below code defines the ThenBy operator in LINQ.

Module Module1

    Sub Main()

        Dim names As String() = {"Rohatash", "Monu", "Ajay", "Vijay", "Ram", "Hari", "vikash", "ravi", "sarvan", "kavita"}

        Dim query As IEnumerable(Of String) = names.OrderBy(Function(s) s.Length).ThenBy(Function(s) s)

        For Each s As [String] In query

            Console.WriteLine(s)

        Next

    End Sub

End Module

The above code produces the following output.

tb1.gif
ANY operator
The Any operator enumerates the source sequence and returns true if any element
 satisfies the test given by the predicate.
For example
The below example defines the the array of numbers. if the sequence has 7 then 
returns true otherwise false.

Module Module1

    Sub Main()

    Dim anynumber As Boolean = New Integer() {2, 3, 4, 5, 6, 7, 8}.Any(Function(n) n = 7)

        Console.WriteLine(anynumber)

    End Sub

End Module

OUTPUT

tb2.gif
The below example defines the the array of numbers. the source sequence and returns false
 if any element does not satisfie the test given by the predicate.

Module Module1

    Sub Main()

    Dim anynumber As Boolean = New Integer() {2, 3, 4, 5, 6, 7, 8}.Any(Function(n) n = 9)

        Console.WriteLine(anynumber)

    End Sub

End Module

OUTPUT

tb3.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.