C Sharp LINQ 投影
外观
C# LINQ 投影是 LINQ(Language Integrated Query)中的一个核心概念,它允许开发者从数据源中选择特定的属性或转换数据,生成一个新的结果集。投影操作通常通过 Select 和 SelectMany 方法实现,它们能够灵活地提取、重组或计算数据,而无需修改原始数据源。
介绍[编辑 | 编辑源代码]
在 LINQ 中,投影(Projection)指的是从一个集合中选择或转换数据,生成一个新的形式。例如,从一个包含学生对象的列表中提取学生的姓名,或计算一组数字的平方值。投影操作不会修改原始数据,而是返回一个新的序列。
主要方法[编辑 | 编辑源代码]
- Select:用于从数据源中选择单个属性或计算新值。
- SelectMany:用于处理嵌套集合(如列表的列表),并将其“展平”为单一序列。
Select 方法[编辑 | 编辑源代码]
Select 是 LINQ 中最常用的投影方法,它接受一个 lambda 表达式作为参数,指定如何转换或提取数据。
基本语法[编辑 | 编辑源代码]
IEnumerable<TResult> result = collection.Select(element => transformation);
示例 1:提取属性[编辑 | 编辑源代码]
假设有一个 Student 类,我们想从学生列表中提取所有学生的姓名:
class Student {
public string Name { get; set; }
public int Age { get; set; }
}
List<Student> students = new List<Student> {
new Student { Name = "Alice", Age = 20 },
new Student { Name = "Bob", Age = 22 },
new Student { Name = "Charlie", Age = 21 }
};
// 使用 Select 提取 Name 属性
IEnumerable<string> names = students.Select(s => s.Name);
foreach (string name in names) {
Console.WriteLine(name);
}
输出:
Alice Bob Charlie
示例 2:计算新值[编辑 | 编辑源代码]
我们可以使用 Select 对数据进行计算,例如计算每个数字的平方:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> squares = numbers.Select(n => n * n);
foreach (int square in squares) {
Console.WriteLine(square);
}
输出:
1 4 9 16 25
SelectMany 方法[编辑 | 编辑源代码]
SelectMany 用于处理嵌套集合,例如一个列表中包含多个子列表,我们希望将所有子列表的元素合并成一个单一序列。
基本语法[编辑 | 编辑源代码]
IEnumerable<TResult> result = collection.SelectMany(element => nestedCollection);
示例 3:展平嵌套列表[编辑 | 编辑源代码]
假设有一个包含多个课程列表的学生类,我们想获取所有课程名称:
class Student {
public string Name { get; set; }
public List<string> Courses { get; set; }
}
List<Student> students = new List<Student> {
new Student { Name = "Alice", Courses = new List<string> { "Math", "Physics" } },
new Student { Name = "Bob", Courses = new List<string> { "Chemistry", "Biology" } }
};
// 使用 SelectMany 展平所有课程
IEnumerable<string> allCourses = students.SelectMany(s => s.Courses);
foreach (string course in allCourses) {
Console.WriteLine(course);
}
输出:
Math Physics Chemistry Biology
示例 4:结合 Select 和 SelectMany[编辑 | 编辑源代码]
我们可以进一步处理嵌套数据,例如获取每个学生及其课程:
var studentCourses = students.SelectMany(
s => s.Courses,
(student, course) => $"{student.Name} - {course}"
);
foreach (var item in studentCourses) {
Console.WriteLine(item);
}
输出:
Alice - Math Alice - Physics Bob - Chemistry Bob - Biology
实际应用场景[编辑 | 编辑源代码]
LINQ 投影在以下场景中非常有用:
1. 数据转换:从数据库查询结果中提取特定字段。 2. API 响应处理:过滤或重组 JSON 数据。 3. 报表生成:计算汇总数据或格式化输出。
案例:从员工数据中提取部门名称[编辑 | 编辑源代码]
假设有一个员工列表,每个员工属于某个部门,我们想获取所有不重复的部门名称:
class Employee {
public string Name { get; set; }
public string Department { get; set; }
}
List<Employee> employees = new List<Employee> {
new Employee { Name = "John", Department = "HR" },
new Employee { Name = "Jane", Department = "IT" },
new Employee { Name = "Doe", Department = "HR" }
};
// 使用 Select 和 Distinct 获取唯一部门
IEnumerable<string> departments = employees
.Select(e => e.Department)
.Distinct();
foreach (string dept in departments) {
Console.WriteLine(dept);
}
输出:
HR IT
性能考虑[编辑 | 编辑源代码]
- 延迟执行:LINQ 投影操作通常是延迟执行的,只有在迭代结果时才会计算。
- 内存效率:使用 Select 和 SelectMany 不会立即创建新集合,而是按需生成数据。
- 优化查询:在数据库查询(如 Entity Framework)中,投影可以减少数据传输量,仅获取所需字段。
总结[编辑 | 编辑源代码]
- Select 用于提取或转换单个元素。
- SelectMany 用于处理嵌套集合并展平结果。
- 投影操作不会修改原始数据,而是生成新的序列。
- 在实际开发中,合理使用投影可以提高代码可读性和性能。