VB.Net program to demonstrate the LINQ Count() function using LINQ query syntax

Here, we are going to demonstrate the LINQ Count() function using LINQ query syntax in VB.Net.
Submitted by Nidhi, on January 27, 2021 [Last updated : March 08, 2023]

VB.Net – LINQ Count() Function

In this program, we will use Count() function in LINQ query syntax. Here we will count the employees whose age is greater than 21.

Program/Source Code:

The source code to demonstrate the LINQ Count() function using LINQ query syntax is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of LINQ Count() function using LINQ query syntax

'VB.NET program to demonstrate the LINQ Count() 
'function using LINQ query syntax.

Imports System
Imports System.IO
Imports System.Linq

Module Module1
    Public Class Employee
        Public id As Integer
        Public name As String
        Public age As Integer
        Public salary As Integer
    End Class

    Sub Main()
        Dim empList = New List(Of Employee) From
        {
            New Employee() With {.id = 101, .name = "Amit", .age = 21, .salary = 5000},
            New Employee() With {.id = 102, .name = "Arun", .age = 22, .salary = 7000},
            New Employee() With {.id = 103, .name = "Aman", .age = 23, .salary = 6000},
            New Employee() With {.id = 104, .name = "Amar", .age = 21, .salary = 6700},
            New Employee() With {.id = 105, .name = "Akki", .age = 22, .salary = 7500},
            New Employee() With {.id = 105, .name = "Anuj", .age = 23, .salary = 8700}
        }

        Dim countEmp = Aggregate emp In empList Into Count(emp.age > 21)

        Console.WriteLine("Count of Employees whose age is greater than 21 : {0}", countEmp)
    End Sub
End Module

Output

Count of Employees whose age is greater than 21 : 4
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains an Employee class and Main() function. The Employee class contains id, name, age, and salary data members.

The Main() function is the entry point for the program. In the Main() function, we created the list of Employees and then count the employees whose age is greater than 21 using LINQ query and then print the result on the console screen.

VB.Net LINQ Query Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.