Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the LINQ query with an extension method
By Nidhi Last Updated : November 11, 2024
VB.Net – LINQ Query with an Extension Method
In this program, we will use a LINQ query with extension method Take() and print 3 elements of the array after sorting the array elements.
Program/Source Code:
The source code to demonstrate the LINQ query with the extension method is given below. The given program is compiled and executed successfully.
VB.Net code to demonstrate the example of LINQ query with an extension method
'VB.NET program to demonstrate the LINQ query
'with an extension method.
Imports System
Imports System.IO
Imports System.Linq
Module Module1
Sub Main()
Dim intVals() As Integer = {50, 20, 40, 35, 10, 27, 60}
Dim result = (From n In intVals
Order By n).Take(3)
Console.WriteLine("Result: ")
For Each val As Integer In result
Console.Write(val & " ")
Next
Console.WriteLine()
End Sub
End Module
Output
Result:
10 20 27
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.
In the Main() function, we created an integer array that contains 7 elements. Here, we used LINQ query with extension method Take() to print the first 3 elements after sorting the array elements on the console screen.
VB.Net LINQ Query Programs »