Linq Count and Range operator using VB.NET

This article describes the Count and Range operator in Linq.
  • 3171

Count Operator

The Count operator counts the number of elements in a sequence, or the number of elements that pass a predicate function.

Understanding count

The below defines the cont can be an array of integer numbers Here is an example of cont.

Dim  cont As Integer = New Integer() {5, 6, 7}.Count()

For Example

Module Module1

    Sub Main()

        Dim Cont As Integer = New Integer() {5, 6, 7, 8, 4, 3}.Count()

     Console.WriteLine(" The number of element :" & cont)

     End Sub

End Module

OUTPUT

 

c1.gif


Aggregating Count with Conditional

 

Module Module1

    Sub Main()

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

        Dim oddNum = Aggregate num In numbers Into Count(num Mod 2 = 1)

        Console.WriteLine("There are " & oddNum & " odd numbers in the list.")

    End Sub

End Module

 

OUTPUT

 

c2.gif
 

Range operator

 

Range works only with integers. Range operator generates a sequence of integral numbers within a specified range.

 

For example

 

Module Module1

    Sub Main()

        For Each i As Integer In Enumerable.Range(7, 5)

            Console.Write(i & " ")

        Next

    End Sub

End Module

 

The above example defines that 7 is the starting point and 5 is the specified range.

 

OUTPUT
 

c3.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.