Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 8 章 LINQ 查询 LINQ 查询

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

LINQ 或 Language Integrated Query(语言集成查询)是一套语言和运行时功能,用于编写对本地对象集合和远程数据源的结构化类型安全查询。

LINQ 使您能够查询任何实现IEnumerable<T> 的集合,无论是数组、列表还是 XML 文档对象模型(DOM),以及远程数据源,如 SQL Server 数据库中的表。LINQ 具有编译时类型检查和动态查询组合的优点。

本章介绍 LINQ 体系结构和编写查询的基础知识。所有核心类型都定义在System.LinqSystem.Linq.Expressions 命名空间中。

备注

本章和随后两章中的示例已预装到名为 LINQPad 的交互式查询工具中。您可以从http://www.linqpad.net 下载 LINQPad

入门

LINQ 的基本数据单元是序列元素。序列是实现 的任何对象,元素是序列中的每个项。在下面的示例中, 是序列, 、 和 是IEnumerable<T> names "Tom" "Dick" "Harry" 元素

string[] names = { "Tom", "Dick", "Harry" };

我们称其为本地序列,因为它代表内存中的本地对象集合。

查询操作符是一种转换序列的方法。典型的查询操作符接受一个输入序列,并输出一个转换后的输出序列。在System.LinqEnumerable 类中,有大约 40 个查询操作符--全部作为静态扩展方法实现。这些运算符被称为标准查询运算符

备注

对本地序列进行操作的查询称为本地查询或LINQ 到对象查询。

LINQ 还支持可从远程数据源(如 SQL Server 数据库)动态输入的序列。这些序列另外还实现了IQueryable<T> 接口,并通过Queryable 类中的一组标准查询操作符得到支持。我们将在"解释查询 "中对此作进一步讨论。

查询是一种表达式,在枚举时,用查询运算符转换序列。最简单的查询由一个输入序列和一个操作符组成。例如,我们可以在一个简单数组上应用Where 操作符,提取长度至少为 4 个字符的字符串,如下所示:

string[] names = { "Tom", "Dick", "Harry" };
IEnumerable<string> filteredNames = System.Linq.Enumerable.Where
                                    (names, n => n.Length >= 4);
foreach (string n in filteredNames)
  Console.WriteLine (n);

Dick
Harry

因为标准查询操作符是作为扩展方法实现的,所以我们可以直接在names 上调用Where ,就好像它是一个实例方法一样:

IEnumerable<string> filteredNames = names.Where (n => n.Length >= 4);

要编译此代码,必须导入System.Linq 命名空间。下面是一个完整的示例:

using System;
using System.Collections.Generic;
using System.Linq;

string[] names = { "Tom", "Dick", "Harry" };

IEnumerable<string> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038