VB.net Program to Demonstrate the Groupby() Linq Extension Method

You are currently viewing VB.net Program to Demonstrate the Groupby() Linq Extension Method

VB.Net program to demonstrate the GroupBy() LINQ extension method

'VB.NET program to demonstrate the 
'GroupBy() LINQ extension method.

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
    End Class

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

        Dim Emps = empList.GroupBy(Function(e) e.age)

        For Each emp In Emps
            Console.WriteLine("Age Group: {0}", emp.Key)

            For Each e In emp.AsEnumerable()
                Console.WriteLine(vbTab & "Employee Name: {0}", e.name)
            Next
        Next
    End Sub
End Module
Age Group: 21
        Employee Name: Amit
        Employee Name: Amar
Age Group: 22
        Employee Name: Arun
        Employee Name: Akki
Age Group: 23
        Employee Name: Aman
        Employee Name: Anuj
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply