首页 / 教程 / LINQ教程

LINQ教程 (更新中...)

关于教程

LINQ是英文Language Integrated Query的缩写,中文译为语言集成查询,发音"link"(我们听到有部分人发作"link-queue"其实是不正确的)。

LINQ是微软的一项技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,于2007年随.NET 3.5和Visual Studio 2008中引入的一种功能强大的查询语言。

LINQ可以与C#或Visual Basic、F#等语言一起从不同的数据源查询数据。

作为开发人员,理解LINQ是非常重要的,因为在大多数实时应用程序中,你将使用LINQ执行不同类型的操作。在本系列教程中,我们将使用C#语言编写LINQ查询。

《LINQ教程》将帮助你从基本到高级,从入门到实战,由浅入深地系统学习LINQ语言。这些教程分为一系列相关的主题,让你从一个必须先理解的主题开始,然后循序渐进地学习LINQ的其他特性。《LINQ教程》包含了大量易于理解的真实示例,帮助读者朋友们将知识点迅速转化到实际生产。

在这个《LINQ教程》系列文章中,我们将涵盖C#中LINQ的所有基本和高级概念。

前提条件

由于本教程系列并不是入门级的,所以在进行本教程学习之前,你需要准备或者掌握:

  • C# 3.5或以上版本的基础知识
  • Visual Studio 2019或以上版本(或者VS Code,Rider等)IDE
  • .NET 5或以上版本SDK

本系列教程用到的示例模型(类)及模拟数据:

/// <summary> /// 订单实体 /// </summary> public class Order { /// <summary> /// ID /// </summary> public int Id { get; set; } /// <summary> /// 客户名称 /// </summary> public string Customer { get; set; } /// <summary> /// 订单金额 /// </summary> public double Price { get; set; } /// <summary> /// 订单来源 /// </summary> public string Source { get; set; } /// <summary> /// 下单时间 /// </summary> public DateTime CreatedAt { get; set; } /// <summary> /// 收货地址 /// </summary> public ShippingAddress Address { get; set; } } public class Customer { public Customer() { } public Customer(int id, int age, string gender, string name, int addressId) { Id = id; Age = age; Gender = gender; Name = name; AddressId = addressId; } public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } public int AddressId { get; set; } } /// <summary> /// 收货地址 /// </summary> public class ShippingAddress { public int Id { get; set; } /// <summary> /// 省份 /// </summary> public string Province { get; set; } /// <summary> /// 城市 /// </summary> public string City { get; set; } /// <summary> /// 地区 /// </summary> public string District { get; set; } /// <summary> /// 街道 /// </summary> public string Street { get; set; } } /// <summary> /// 模拟数据 /// </summary> public static class FakeData { /// <summary> /// 模拟订单集合数据 /// </summary> public static List<Order> Orders => new() { new Order { Id = 1, Customer = "Rector", Price = 3699.00D, Source = "线上", CreatedAt = new DateTime(2021, 5, 1, 20, 30, 25), Address = Address1 }, new Order { Id = 2, Customer = "James", Price = 2699.00D, Source = "线上", CreatedAt = new DateTime(2021, 6, 15, 18, 06, 05), Address = Address2 }, new Order { Id = 3, Customer = "Chris", Price = 1999.00D, Source = "线下", CreatedAt = new DateTime(2021, 7, 21, 20, 10, 02), Address = Address3 }, new Order { Id = 4, Customer = "Steven", Price = 5699.00D, Source = "线上", CreatedAt = new DateTime(2021, 8, 30, 09, 30, 25), Address = Address4 }, new Order { Id = 5, Customer = "Jo", Price = 2569.00D, Source = "线下", CreatedAt = new DateTime(2021, 9, 11, 10, 28, 25), Address = Address1 }, new Order { Id = 6, Customer = "Rector", Price = 5699.00D, Source = "线上", CreatedAt = new DateTime(2021, 10, 16, 16, 30, 08), Address = Address1 } }; public static List<Customer> Customers => new() { new Customer(id: 1, age: 20, gender: "男", name: "Rector", addressId: 1), new Customer(id: 2, age: 24, gender: "女", name: "Anna", addressId: 2), new Customer(id: 3, age: 26, gender: "女", name: "Xi", addressId: 1), new Customer(id: 4, age: 30, gender: "男", name: "Curry", addressId: 5) }; public static ShippingAddress Address1 => new() { Id = 1, Province = "重庆", City = "重庆", District = "渝北区", Street = "回兴街道" }; public static ShippingAddress Address2 => new() { Id = 2, Province = "重庆", City = "重庆", District = "江北区", Street = "观音桥街道" }; public static ShippingAddress Address3 => new() { Id = 3, Province = "四川", City = "成都", District = "金牛区", Street = "西华街道" }; public static ShippingAddress Address4 => new() { Id = 4, Province = "四川", City = "成都", District = "青羊区", Street = "草市街街道" }; public static ShippingAddress Address5 => new() { Id = 5, Province = "上海", City = "上海", District = "黄浦区", Street = "外滩街道" }; public static List<ShippingAddress> Addresses => new() { Address1, Address2, Address3, Address4, Address5 }; }

注:示例模型仅为了演示,不代表实际生产的代码和逻辑(仅供学习参考)。

教程目录