编辑
2025-09-28
C#
00

目录

过滤集合
示例1:使用Where过滤集合
示例2:使用多个条件过滤集合
排序集合
示例3:使用OrderBy排序集合
示例4:使用OrderByDescending排序集合
示例5:结合过滤和排序
排序复杂对象
示例6:按对象属性排序
示例7:内连接查询
示例8:组连接查询
示例9:左外连接查询
结论

在C#中,LINQ(Language Integrated Query)是一种强大的数据查询语言,它允许开发者以声明式的方式对集合进行查询、过滤和排序。在本文中,我们将探讨如何使用LINQ来过滤和排序各种类型的集合。

过滤集合

过滤是指从集合中选择满足特定条件的元素。在LINQ中,Where扩展方法用于过滤集合。

示例1:使用Where过滤集合

C#
using System.Diagnostics; namespace AppLinq { internal class Program { static void Main(string[] args) { List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var evenNumbers = numbers.Where(n => n % 2 == 0); foreach (var num in evenNumbers) { Console.WriteLine(num); } Console.ReadKey(); } } }

image.png

在上面的例子中,我们使用Where方法和一个lambda表达式来选择集合中的偶数。

示例2:使用多个条件过滤集合

C#
using System.Diagnostics; namespace AppLinq { internal class Program { static void Main(string[] args) { List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var filteredNumbers = numbers.Where(n => n > 3 && n < 8); foreach (var num in filteredNumbers) { Console.WriteLine(num); } Console.ReadKey(); } } }

image.png

在这个例子中,我们使用了两个条件来过滤集合,选择大于3且小于8的数字。

排序集合

排序是指按照特定的顺序重新排列集合中的元素。在LINQ中,OrderByOrderByDescending方法用于对集合元素进行排序。

示例3:使用OrderBy排序集合

C#
using System.Diagnostics; namespace AppLinq { internal class Program { static void Main(string[] args) { List<int> numbers = new List<int> { 7, 2, 5, 3, 9, 1, 6, 4, 8 }; var sortedNumbers = numbers.OrderBy(n => n); foreach (var num in sortedNumbers) { Console.WriteLine(num); } Console.ReadKey(); } } }

image.png

在上面的例子中,我们使用OrderBy方法对数字进行升序排序。

示例4:使用OrderByDescending排序集合

C#
List<int> numbers = new List<int> { 7, 2, 5, 3, 9, 1, 6, 4, 8 }; var sortedNumbersDesc = numbers.OrderByDescending(n => n); foreach (var num in sortedNumbersDesc) { Console.WriteLine(num); // 输出 9, 8, 7, 6, 5, 4, 3, 2, 1 }

在这个例子中,我们使用OrderByDescending方法对数字进行降序排序。

示例5:结合过滤和排序

C#
using System.Diagnostics; namespace AppLinq { internal class Program { static void Main(string[] args) { List<int> numbers = new List<int> { 7, 2, 5, 3, 9, 1, 6, 4, 8 }; var sortedNumbersDesc = numbers.OrderByDescending(n => n); foreach (var num in sortedNumbersDesc) { Console.WriteLine(num); } Console.ReadKey(); } } }

在上面的例子中,我们首先过滤出大于3的数字,然后对这些数字进行升序排序。

排序复杂对象

当我们处理的不是简单的数字或字符串集合时,我们可以按照对象的属性进行排序。

示例6:按对象属性排序

C#
using System.Diagnostics; namespace AppLinq { public class Person { public string Name { get; set; } public int Age { get; set; } } internal class Program { static void Main(string[] args) { List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 }, new Person { Name = "Charlie", Age = 35 } }; var sortedPeople = people.OrderBy(p => p.Age); foreach (var person in sortedPeople) { Console.WriteLine($"{person.Name}: {person.Age}"); } Console.ReadKey(); } } }

image.png

在这个例子中,我们按照人的年龄进行升序排序。

在C#中,LINQ允许我们使用join操作符来执行多数据源之间的关联查询。这类似于SQL中的JOIN操作,可以让我们根据一定的条件将不同数据源中的数据组合在一起。

示例7:内连接查询

假设我们有两个集合,一个是员工集合,另一个是部门集合。我们想要根据部门ID将员工与其所在的部门关联起来。

C#
using System.Diagnostics; namespace AppLinq { public class Employee { public string Name { get; set; } public int DepartmentId { get; set; } } public class Department { public int Id { get; set; } public string Name { get; set; } } internal class Program { static void Main(string[] args) { List<Employee> employees = new List<Employee> { new Employee { Name = "Alice", DepartmentId = 1 }, new Employee { Name = "Bob", DepartmentId = 2 }, new Employee { Name = "Charlie", DepartmentId = 1 } }; List<Department> departments = new List<Department> { new Department { Id = 1, Name = "HR" }, new Department { Id = 2, Name = "IT" } }; var query = from employee in employees join department in departments on employee.DepartmentId equals department.Id select new { EmployeeName = employee.Name, DepartmentName = department.Name }; foreach (var item in query) { Console.WriteLine($"{item.EmployeeName} works in {item.DepartmentName}"); } Console.ReadKey(); } } }

image.png

示例8:组连接查询

如果我们想要按部门分组列出所有员工,可以使用组连接。

C#
using System.Diagnostics; namespace AppLinq { public class Employee { public string Name { get; set; } public int DepartmentId { get; set; } } public class Department { public int Id { get; set; } public string Name { get; set; } } internal class Program { static void Main(string[] args) { List<Employee> employees = new List<Employee> { new Employee { Name = "Alice", DepartmentId = 1 }, new Employee { Name = "Bob", DepartmentId = 2 }, new Employee { Name = "Charlie", DepartmentId = 1 } }; List<Department> departments = new List<Department> { new Department { Id = 1, Name = "HR" }, new Department { Id = 2, Name = "IT" } }; var query = from department in departments join employee in employees on department.Id equals employee.DepartmentId into deptEmployees select new { DepartmentName = department.Name, Employees = deptEmployees }; foreach (var dept in query) { Console.WriteLine(dept.DepartmentName); foreach (var emp in dept.Employees) { Console.WriteLine($" {emp.Name}"); } } Console.ReadKey(); } } }

image.png

示例9:左外连接查询

有时候,我们想要包含左边数据源中的所有元素,即使它们在右边数据源中没有匹配项。这可以通过使用DefaultIfEmpty方法实现左外连接。

C#
using System.Diagnostics; namespace AppLinq { public class Employee { public string Name { get; set; } public int DepartmentId { get; set; } } public class Department { public int Id { get; set; } public string Name { get; set; } } internal class Program { static void Main(string[] args) { List<Employee> employees = new List<Employee> { new Employee { Name = "Alice", DepartmentId = 1 }, new Employee { Name = "Bob", DepartmentId = 2 }, new Employee { Name = "Charlie", DepartmentId = 1 } }; List<Department> departments = new List<Department> { new Department { Id = 1, Name = "HR" }, new Department { Id = 2, Name = "IT" } }; var query = from employee in employees join department in departments on employee.DepartmentId equals department.Id into deptGroup from dept in deptGroup.DefaultIfEmpty() select new { EmployeeName = employee.Name, DepartmentName = dept?.Name ?? "No Department" }; foreach (var item in query) { Console.WriteLine($"{item.EmployeeName} works in {item.DepartmentName}"); } Console.ReadKey(); } } }

image.png

在这个例子中,即使某个员工没有对应的部门,我们也能够获取到该员工的信息,并且标记为"No Department"。

这些示例展示了如何使用LINQ进行多数据源的关联查询,包括内连接、组连接和左外连接。通过这些查询,我们可以有效地组合和操作来自不同数据源的数据。

结论

LINQ查询提供了一种优雅和强大的方式来处理C#集合的过滤和排序。通过使用WhereOrderByOrderByDescending等方法,我们可以轻松地对数据进行查询和操作。重要的是要注意,LINQ查询默认是延迟执行的,这意味着查询不会立即执行,直到我们开始迭代结果时。这使得LINQ查询非常灵活和高效,但也需要我们在修改原始数据时保持警惕。通过上面的例子,我们可以看到LINQ如何帮助我们编写更清晰、更简洁的代码来处理集合数据。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!