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 核心要点

第 9 章 LINQ 操作符 LINQ 操作符

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

本章将逐一介绍 LINQ 查询操作符。其中"投影 ""连接 "两节除了作为参考外,还涵盖了一些概念领域:

  • 投影对象层次

  • Select,SelectMany,Join, 和GroupJoin

  • 使用多个范围变量的查询表达式

本章所有示例都假定names 数组的定义如下:

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

查询数据库的示例假定名为dbContext 的变量实例化为

var dbContext = new NutshellContext();

其中NutshellContext 的定义如下

public class NutshellContext : DbContext
{
  public DbSet<Customer> Customers { get; set; }
  public DbSet<Purchase> Purchases { get; set; }

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Customer>(entity =>
    {
      entity.ToTable("Customer");
      entity.Property(e => e.Name).IsRequired();  // Column is not nullable
    });
    modelBuilder.Entity<Purchase>(entity =>
    {
      entity.ToTable("Purchase");
      entity.Property(e => e.Date).IsRequired();     
      entity.Property(e => e.Description).IsRequired();     
    });
  }
}

public class Customer
{
  public int ID { get; set; }
  public string Name { get; set; }

  public virtual List<Purchase> Purchases { get; set; }
    = new List<Purchase>();
}

public class Purchase
{        
  public int ID { get; set; }
  public int? CustomerID { get; set; }
  public DateTime Date { get; set; }
  public string Description { get; set; }
  public decimal Price { get; set; }

  public virtual Customer Customer { get; set; }
}
备注

本章中的所有示例都已预装到 LINQPad 中,同时还预装了一个具有匹配模式的示例数据库。您可以从http://www.linqpad.net 下载 LINQPad

下面是相应的 SQL Server 表定义:

 CREATE TABLE Customer ( ID int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(30) ...
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