Home »
VB.Net »
VB.Net Programs
VB.Net program to select specified fields using LINQ query
By Nidhi Last Updated : November 16, 2024
VB.Net – Selecting specified fields using LINQ query
In this program, we will use the LINQ query to select the specified fields. After that print values select fields on the console screen.
Program/Source Code:
The source code to select specified fields using the LINQ query is given below. The given program is compiled and executed successfully.
VB.Net code to select specified fields using LINQ query
'VB.NET program to select specified columns
'using LINQ query.
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 selectResult = From emp In empList
Select New With {.name = emp.name, .age = emp.age}
Console.WriteLine("Employees: ")
For Each emp In selectResult
Console.WriteLine(vbTab & emp.name & " " & emp.age)
Next
End Sub
End Module
Output
Employees:
Amit 21
Arun 22
Aman 23
Amar 21
Akki 22
Anuj 23
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains the Employee class and Main() function. The Employee class contains three data members id, name, and age.
The Main() function is the entry point for the program. In the Main() function we created the list of employees. Here, we selected two column names and age from the employee list using the Select clause in the LINQ query and then printed the employee detail on the console screen.
VB.Net LINQ Query Programs »