LINQ to Objects (C#)LINQ to Objects (C#)

术语“LINQ to Objects”指直接将 LINQ 查询与任何 IEnumerableIEnumerable<T> 集合一起使用,而不使用中间 LINQ 提供程序或 API,例如 LINQ to SQLLINQ to XMLThe 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. 采用旧方法,必须编写指定如何从集合检索数据的复杂的 foreach 循环。In the old way, you had to write complex foreach 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 查询与传统 foreach 循环相比具有三大优势:In addition, LINQ queries offer three main advantages over traditional foreach 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 和字符串 (C#)LINQ and Strings (C#)
阐释如何使用 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 和反射 (C#)LINQ and Reflection (C#)
指向演示 LINQ 如何使用反射的示例的链接。Links to a sample that demonstrates how LINQ uses reflection.

LINQ 和文件目录 (C#)LINQ and File Directories (C#)
阐释如何使用 LINQ 来与文件系统进行交互。Explains how LINQ can be used to interact with file systems. 还包括指向演示这些概念的主题的链接。Also includes links to topics that demonstrate these concepts.

如何:使用 LINQ 查询 ArrayList (C#)How to: Query an ArrayList with LINQ (C#)
演示如何使用 C# 查询 ArrayList。Demonstrates how to query an ArrayList in C#.

如何:为 LINQ 查询添加自定义方法 (C#)How to: Add Custom Methods for LINQ Queries (C#)
阐释如何通过向 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.

语言集成查询 (LINQ) (C#)Language-Integrated Query (LINQ) (C#)
提供指向阐释 LINQ 并提供执行查询的代码示例的主题的链接。Provides links to topics that explain LINQ and provide examples of code that perform queries.