Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the custom aggregation using LINQ Aggregate() method
By Nidhi Last Updated : November 11, 2024
VB.Net – Custom aggregation using LINQ Aggregate() Method
In this program, we will use the Aggregate() method to perform custom aggregation on specified list elements, here we will perform aggregation on integers and strings.
Program/Source Code:
The source code to demonstrate the custom aggregation using LINQ Aggregate() method is given below. The given program is compiled and executed successfully.
VB.Net code to demonstrate the example of custom aggregation using LINQ Aggregate() method
'VB.NET program to demonstrate the custom aggregation
'using LINQ Aggregate() method.
Imports System
Imports System.IO
Imports System.Linq
Module Module1
Sub Main()
Dim countryList As IList(Of String) = New List(Of String) From {
"India",
"USA",
"UK",
"CANADA",
"SRI-LANKA"
}
Dim Countries = countryList.Aggregate(Function(c1, c2) c1 + ", " + c2)
Console.WriteLine("Country names separated by comma:")
Console.WriteLine(Countries)
Dim Marks() As Integer = {67, 78, 45, 88, 91}
Dim MarksTotal = Marks.Aggregate(Function(m1, m2) m1 + m2)
Console.WriteLine("Marks Total: " & MarksTotal)
End Sub
End Module
Output
Country names separated by comma:
India, USA, UK, CANADA, SRI-LANKA
Marks Total: 369
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains a Main() function. The Main() function is the entry point for the program.
In the Main() function, We created the list of countries and the list of Marks of a student. Here, we used the Aggregate() method to separate countries using a comma and perform the addition of marks and get the total of marks. After that, we printed the result on aggregation on the console screen.
VB.Net LINQ Query Programs »