在C#开发过程中,LINQ(语言集成查询)为开发者提供了统一的数据查询方式,其中LINQ to Objects是面向内存中集合对象的查询方案,支持对数组、列表等实现了IEnumerable接口的对象进行高效查询。它不需要额外的查询引擎,直接基于.NET运行时提供的功能实现数据筛选、排序、分组等操作,是日常业务开发里使用频率很高的功能。

LINQ to Objects的基础使用方式
LINQ to Objects支持两种语法形式,分别是查询语法和方法语法,两者最终都会被编译器转换为方法调用,功能上基本等价,开发者可以根据个人习惯选择使用。
查询语法示例
查询语法更接近SQL的书写风格,可读性较强,适合复杂的查询逻辑。以下是一个从整数列表中筛选偶数的示例:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 查询语法:筛选偶数
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
Console.WriteLine("偶数结果:");
foreach (var num in evenNumbers)
{
Console.Write(num + " ");
}
// 输出:2 4 6 8 10
}
}
方法语法示例
方法语法基于扩展方法实现,链式调用的风格更适合简单的查询逻辑,以下是同样筛选偶数的方法语法实现:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 方法语法:筛选偶数
var evenNumbers = numbers.Where(num => num % 2 == 0);
Console.WriteLine("偶数结果:");
foreach (var num in evenNumbers)
{
Console.Write(num + " ");
}
// 输出:2 4 6 8 10
}
}
常用LINQ to Objects查询方法
除了基础的Where筛选方法,LINQ还提供了多种常用的查询方法,覆盖排序、分组、投影、聚合等场景。
排序方法
OrderBy和OrderByDescending用于升序和降序排序,ThenBy和ThenByDescending用于多条件排序。以下示例对学生列表按年龄升序、分数降序排序:
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public string Name { get; set; }
public int Age { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main()
{
List<Student> students = new List<Student>
{
new Student { Name = "张三", Age = 18, Score = 85 },
new Student { Name = "李四", Age = 17, Score = 90 },
new Student { Name = "王五", Age = 18, Score = 92 }
};
// 按年龄升序,年龄相同按分数降序排序
var sortedStudents = students.OrderBy(s => s.Age).ThenByDescending(s => s.Score);
Console.WriteLine("排序后的学生列表:");
foreach (var student in sortedStudents)
{
Console.WriteLine($"姓名:{student.Name},年龄:{student.Age},分数:{student.Score}");
}
}
}
分组方法
GroupBy可以按照指定条件对数据进行分组,返回分组后的结果集合。以下示例将学生按年龄分组:
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
List<Student> students = new List<Student>
{
new Student { Name = "张三", Age = 18 },
new Student { Name = "李四", Age = 17 },
new Student { Name = "王五", Age = 18 }
};
// 按年龄分组
var groups = students.GroupBy(s => s.Age);
Console.WriteLine("分组结果:");
foreach (var group in groups)
{
Console.WriteLine($"年龄:{group.Key},成员:{string.Join("、", group.Select(s => s.Name))}");
}
}
}
聚合方法
LINQ提供了Count、Sum、Average、Max、Min等聚合方法,用于统计数据。以下示例计算学生分数的相关统计值:
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main()
{
List<Student> students = new List<Student>
{
new Student { Name = "张三", Score = 85 },
new Student { Name = "李四", Score = 90 },
new Student { Name = "王五", Score = 92 }
};
Console.WriteLine($"学生总数:{students.Count()}");
Console.WriteLine($"总分:{students.Sum(s => s.Score)}");
Console.WriteLine($"平均分:{students.Average(s => s.Score)}");
Console.WriteLine($"最高分:{students.Max(s => s.Score)}");
Console.WriteLine($"最低分:{students.Min(s => s.Score)}");
}
}
使用注意事项
- LINQ查询默认是延迟执行的,只有当遍历查询结果时,才会真正执行查询逻辑,如果需要立即执行,可以调用
ToList、ToArray等方法。 - 查询语法的可读性更强,适合复杂的多条件查询;方法语法更灵活,适合简单的链式调用场景,开发者可以根据实际需求选择。
- 在使用Lambda表达式作为查询条件时,注意避免复杂的逻辑嵌套,否则会降低代码的可读性。
LINQ C# LINQ_to_Objects 查询方法修改时间:2026-07-02 03:48:17