VB.NET语言 LINQ to Objects

VB.NET阿木 发布于 2025-05-28 18 次阅读


LINQ to Objects in VB.NET: A Comprehensive Guide

Introduction

LINQ (Language Integrated Query) is a powerful feature introduced in .NET 3.5 that allows developers to write queries in a declarative manner against various data sources. LINQ to Objects is one of the most fundamental LINQ providers, enabling developers to query collections of objects directly from the .NET Framework. This article will delve into the intricacies of LINQ to Objects in VB.NET, covering its core concepts, syntax, and practical examples.

Understanding LINQ to Objects

LINQ to Objects is designed to work with in-memory collections such as arrays, lists, and collections. It provides a set of standard query operators that can be used to perform operations like filtering, sorting, grouping, and aggregating data. These operators are part of the `System.Linq` namespace and can be used with any collection that implements the `IEnumerable` interface.

Core Concepts

Before diving into the code, let's understand some of the core concepts of LINQ to Objects:

1. Query Expression: A query expression is a sequence of method calls that represents a LINQ query. It is written in a declarative manner, specifying what to do rather than how to do it.

2. Method Syntax: Method syntax is a way to write LINQ queries using method calls. It is more verbose but provides a clear mapping to the underlying methods.

3. Query Syntax: Query syntax is a more readable way to write LINQ queries using a SQL-like syntax. It is syntactic sugar over the method syntax and provides a more intuitive way to express queries.

4. Execution: LINQ queries are executed in two phases: the query plan is generated, and then the data is retrieved and processed.

LINQ to Objects Syntax

Method Syntax

Here's an example of a LINQ to Objects query using method syntax to filter a list of integers:

vb.net
Imports System.Linq

Module Module1
Sub Main()
Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Dim evenNumbers = From num In numbers
Where num Mod 2 = 0
Select num

For Each number In evenNumbers
Console.WriteLine(number)
Next
End Sub
End Module

Query Syntax

The same query using query syntax would look like this:

vb.net
Imports System.Linq

Module Module1
Sub Main()
Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Dim evenNumbers = (From num In numbers
Where num Mod 2 = 0
Select num).ToList()

For Each number In evenNumbers
Console.WriteLine(number)
Next
End Sub
End Module

LINQ to Objects Operators

LINQ to Objects provides a rich set of operators that can be used to manipulate collections. Here are some of the commonly used operators:

1. Where: Filters elements based on a specified condition.

2. Select: Projects each element of a collection into a new form.

3. OrderBy: Sorts the elements of a collection in ascending order.

4. OrderByDescending: Sorts the elements of a collection in descending order.

5. GroupBy: Groups the elements of a collection based on a specified key.

6. Aggregate: Performs a bitwise reduction on the elements of a collection.

7. Concat: Concatenates two sequences into a single sequence.

8. Union: Removes duplicates from the result of the concatenation of two sequences.

Practical Examples

Let's look at some practical examples to illustrate the use of LINQ to Objects operators.

Filtering and Selecting

vb.net
Imports System.Linq

Module Module1
Sub Main()
Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

' Filter even numbers and select their squares
Dim evenSquares = From num In numbers
Where num Mod 2 = 0
Select num num

For Each square In evenSquares
Console.WriteLine(square)
Next
End Sub
End Module

Sorting

vb.net
Imports System.Linq

Module Module1
Sub Main()
Dim numbers As Integer() = {5, 2, 9, 1, 5, 6}

' Sort numbers in ascending order
Dim sortedNumbers = From num In numbers
OrderBy(num)

For Each number In sortedNumbers
Console.WriteLine(number)
Next
End Sub
End Module

Grouping

vb.net
Imports System.Linq

Module Module1
Sub Main()
Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

' Group numbers by their parity
Dim groupedNumbers = From num In numbers
Group num By num Mod 2 Into Group

For Each group In groupedNumbers
Console.WriteLine("Group: " & group.Key)
For Each number In group
Console.WriteLine(number)
Next
Next
End Sub
End Module

Conclusion

LINQ to Objects is a powerful tool in the VB.NET developer's arsenal, providing a concise and readable way to query in-memory collections. By understanding the core concepts and syntax, as well as the various operators available, developers can write efficient and maintainable code. This article has covered the basics of LINQ to Objects in VB.NET, providing a foundation for further exploration and practical application.