VB.Net program to demonstrate the LINQ Aggregate() method with result selector

Here, we are going to demonstrate the LINQ Aggregate() method with result selector in VB.Net.
Submitted by Nidhi, on January 27, 2021

In this program, we will use the Aggregate() method with result selector, here we separate employee names using comma but here comma will not be printed after the last employee name.

Program/Source Code:

The source code to demonstrate the LINQ Aggregate() method with the result selector is given below. The given program is compiled and executed successfully.

'VB.NET program to demonstrate the LINQ Aggregate() 
'method with result selector.

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 EmployeeNames = empList.Aggregate(Of String, String)(
               String.Empty,
               Function(str, e) str + e.name + ",",
               Function(str) str.Substring(0, str.Length - 1))

        Console.WriteLine("Employee Names:")
        Console.WriteLine(EmployeeNames)
    End Sub
End Module

Output:

Employee Names:
Amit,Arun,Aman,Amar,Akki,Anuj
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, and age data members.

The Main() function is the entry point for the program. In the Main() function, We created the list of Employees and then perform aggregation on employee names to separate using comma. Here, we also remove the comma after the last name of the employee. After that, we printed the result on the Aggregate() method on the console screen.

VB.Net LINQ Query Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.