Using LINQ to Objects in Visual Basic .NET for Cleaner In‑Memory Queries
Learn how Visual Basic .NET’s LINQ to Objects lets you write declarative, readable queries over in‑memory collections while keeping performance comparable to hand‑written loops.
25 Jun 2026, 07:13 UTC

Problem: Repetitive loop code obscures intent
When you need to filter, sort, or project a collection of objects in a VB.NET application, writing explicit For Each loops with nested If statements quickly becomes noisy. This makes the business logic harder to read and increases the chance of mistakes when the query changes.
Thesis: LINQ to Objects lets you express the same operations declaratively while keeping performance comparable to hand‑written loops
By using the LINQ syntax built into Visual Basic .NET, you can describe what you want from an IEnumerable(Of T) without detailing how to iterate. The compiler translates the query into efficient IL, and deferred execution ensures the work happens only when you enumerate the result.
How LINQ to Objects works
- The query expression is compiled into a chain of extension methods (
Where,Select, etc.) onEnumerable. - No data is processed until you iterate over the query (e.g., with
For EachorToList). - Because it operates on any
IEnumerable(Of T), you can apply it to arrays, lists, XML, or custom collections.
Worked example: Finding expensive products
Suppose you have a list of Product objects and you need the names of those priced over 100, sorted by price descending.
Imports System.Linq
Module Program
Sub Main()
Dim products As New List(Of Product) From {
New Product With {.Name = "Widget", .Price = 75},
New Product With {.Name = "Gadget", .Price = 120},
New Product With {.Name = "Doohickey", .Price = 95},
New Product With {.Name = "Thingamajig", .Price = 150}
}
' LINQ query: filter, then project, then sort
Dim expensiveNames = From p In products
Where p.Price > 100
Select p.Name
Order By p.Price Descending
' Enumeration triggers execution
For Each name In expensiveNames
Console.WriteLine(name)
Next
End Sub
End Module
Class Product
Public Property Name As String
Public Property Price As Decimal
End Class
When you run this code, the console will display the names of the products that meet the price condition, ordered from highest to lowest price. The query itself does not iterate the list until the For Each loop begins.
Trade‑off: Debugging visibility
Because the compiler rewrites the query into method chains, setting a breakpoint inside the Where clause does not pause at each element as it would in a manual loop. To inspect intermediate values you can:
- Add a temporary
Selectthat projects the item into a debuggable anonymous type, or - Force early execution with
.ToList()and then examine the list.
This adds a small amount of overhead only when you need debugging; production code can stay deferred for optimal performance.
Actionable closing
Start by replacing one repetitive loop in your codebase with a LINQ query that does the same filtering or projection. Verify the result matches the original loop by comparing outputs, then consider whether deferred execution helps you compose additional operators without extra passes. If you hit memory limits with very large collections, fall back to streaming techniques or database queries, but for typical in‑memory business data LINQ to Objects offers a cleaner, maintainable alternative.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.