Language Integrated Query (LINQ) in VB.NET

Language Integrated Query (LINQ) is a powerful set of technologies in the .NET Framework that adds native data querying capabilities to the .NET programming languages. With LINQ, you can write queries directly in VB.NET against structured information from various sources, such as XML documents, databases, and collections.

What is LINQ?

LINQ provides a consistent, integrated way to query data. It unifies different kinds of data sources and data formats into a common object model. This means you can use the same query syntax to query data from a SQL Server database, an XML file, or an in-memory collection.

Key Features of LINQ

Basic Query Syntax Example

Here's a simple example of how to query a list of numbers using LINQ in VB.NET:

1 Imports System.Linq 2 3 Module LinqExample 4 Sub Main() 5 ' Sample data 6 Dim numbers = New List(Of Integer) { 5, 10, 15, 20, 25 } 7 8 ' LINQ query to select numbers greater than 10 9 Dim filteredNumbers = From num In numbers 10 Where num > 10 11 Select num 12 13 ' Iterate and print the results 14 Console.WriteLine("Numbers greater than 10:") 15 For Each n In filteredNumbers 16 Console.WriteLine(n) 17 Next 18 End Sub 19 End Module

LINQ to Objects

LINQ to Objects allows you to query collections of objects such as List(Of T), arrays, and dictionaries directly within your .NET code. This is often the most straightforward way to start using LINQ.

Common LINQ Operators

Further Reading