LINQ to Objects (Visual Basic)LINQ to Objects (Visual Basic)

詞彙 "LINQ to Objects" 是指直接搭配使用 LINQ 查詢與任何 IEnumerableIEnumerable<T> 集合,而不要使用中繼 LINQ 提供者或 API (例如 LINQ to SQLLINQ to XML)。The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML. 您可以使用 LINQ 查詢任何可列舉的集合,例如 List<T>ArrayDictionary<TKey,TValue>You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>. 這類集合可以是使用者定義,或由 .NET Framework API 所傳回。The collection may be user-defined or may be returned by a .NET Framework API.

基本上,LINQ to Objects 代表使用集合的新方法。In a basic sense, LINQ to Objects represents a new approach to collections. 在舊的方法中,您必須撰寫複雜的 For Each 迴圈,以指定如何從集合擷取資料。In the old way, you had to write complex For Each loops that specified how to retrieve data from a collection. 在 LINQ 方法中,您會撰寫描述所要擷取內容的宣告式程式碼。In the LINQ approach, you write declarative code that describes what you want to retrieve.

此外,LINQ 查詢還提供三種超越傳統 For Each 迴圈的主要優點:In addition, LINQ queries offer three main advantages over traditional For Each loops:

  1. 它們更加簡潔易懂 (尤其是在篩選多個條件時)。They are more concise and readable, especially when filtering multiple conditions.

  2. 它們只要使用最少的應用程式程式碼,即可提供強大的篩選、排序及群組功能。They provide powerful filtering, ordering, and grouping capabilities with a minimum of application code.

  3. 它們只需要一點修改,甚至不用修改,便可以移植到其他資料來源。They can be ported to other data sources with little or no modification.

一般來說,您要對資料執行的作業越複雜,就越能體會使用 LINQ 取代傳統反覆項目技術的好處。In general, the more complex the operation you want to perform on the data, the more benefit you will realize by using LINQ instead of traditional iteration techniques.

本節的目的是要透過一些精選的範例,示範 LINQ 方法,The purpose of this section is to demonstrate the LINQ approach with some select examples. 而不是要提供詳細的說明。It is not intended to be exhaustive.

本節內容In This Section

LINQ 與字串 (Visual Basic)LINQ and Strings (Visual Basic)
說明如何使用 LINQ 查詢及轉換字串與字串集合。Explains how LINQ can be used to query and transform strings and collections of strings. 此外也包含示範這些原理的主題連結。Also includes links to topics that demonstrate these principles.

LINQ 和反映(Visual Basic)LINQ and Reflection (Visual Basic)
示範 LINQ 如何使用反映的範例連結。Links to a sample that demonstrates how LINQ uses reflection.

LINQ 與檔案目錄 (Visual Basic)LINQ and File Directories (Visual Basic)
說明如何使用 LINQ 與檔案系統互動。Explains how LINQ can be used to interact with file systems. 此外也包含示範這些概念的主題連結。Also includes links to topics that demonstrate these concepts.

如何:使用 LINQ 查詢 ArrayList (Visual Basic)How to: Query an ArrayList with LINQ (Visual Basic)
示範如何以 C# 查詢 ArrayList。Demonstrates how to query an ArrayList in C#.

如何:新增 LINQ 查詢的自訂方法(Visual Basic)How to: Add Custom Methods for LINQ Queries (Visual Basic)
說明如何透過將擴充方法加入至 IEnumerable<T> 介面,來延伸您可以用於 LINQ 查詢的方法組。Explains how to extend the set of methods that you can use for LINQ queries by adding extension methods to the IEnumerable<T> interface.

Language-Integrated Query (LINQ) (Visual Basic)Language-Integrated Query (LINQ) (Visual Basic)
提供 LINQ 的主題說明連結,以及執行查詢的程式碼範例。Provides links to topics that explain LINQ and provide examples of code that perform queries.