Visual Basic
LINQ Query Expressions
Query data with VB LINQ syntax.
By EZ4Code Team
linqquery
Code
Dim people = New List(Of Person) From {
New Person With {.Name = "Alice", .Age = 30, .City = "NYC"},
New Person With {.Name = "Bob", .Age = 25, .City = "LA"},
New Person With {.Name = "Carol", .Age = 35, .City = "NYC"}
}
' Query syntax
Dim nycResidents = From p In people
Where p.City = "NYC"
Order By p.Age Descending
Select p.Name, p.Age
For Each r In nycResidents
Console.WriteLine($"{r.Name}: {r.Age}")
Next
' Aggregates
Dim avgAge = people.Average(Function(p) p.Age)
Dim grouped = From p In people
Group p By p.City Into Group
Select City, AvgAge = Group.Average(Function(p) p.Age)
' Method syntax
Dim names = people.Where(Function(p) p.Age > 25).
Select(Function(p) p.Name).
ToList()Explanation
VB LINQ syntax differs slightly from C# — Group By uses Into Group. Both compile to the same IL. Use query syntax for complex joins/groups; method syntax for simple filters. Anonymous types (Select Name, Age) create projections on the fly. LINQ is lazy — use ToList() to force evaluation.
More Visual Basic Snippets
Variables and Types
Declare variables with Dim and type inference.
Loops and Iteration
For, For Each, While, and Do loops in VB.
Sub and Function Procedures
Define Sub (no return) and Function (returns value).
Windows Forms Basics
Create a simple WinForms application.
File I/O
Read/write text files and use My.Computer.FileSystem.
Error Handling (Try/Catch)
Structured exception handling in VB.