下面是关于 C# Linq 的 Join() 的详细攻略及两个样例示例。
Join() 方法概述
Join()
是 C# Linq 中用于将两个集合中符合条件的元素进行连接的方法。它需要两个集合参数和两个 Key Selector 函数,其中一个 Key Selector 用于指定第一个集合中的连接键,另一个 Key Selector 用于指定第二个集合中的连接键。Join()
方法的返回值是一个新集合,其中的元素包含了两个集合中连接键相等的元素对。
Join()
方法的语法如下:
Join<TOuter, TInner, TKey, TResult>(
IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector
)
参数说明:
outer
:第一个集合。inner
:第二个集合。outerKeySelector
:指定第一个集合中的连接键。innerKeySelector
:指定第二个集合中的连接键。resultSelector
:组合两个集合中连接键相等的元素对得到的新元素对象。
示例 1
下面是一个简单的示例,用于演示如何使用 Join()
方法连接两个集合:
// 创建“外部的”集合
List<Department> departments = new List<Department>()
{
new Department { Id = 1, Name = "HR" },
new Department { Id = 2, Name = "Finance" },
new Department { Id = 3, Name = "IT" }
};
// 创建“内部的”集合
List<Employee> employees = new List<Employee>()
{
new Employee { Id = 1, Name = "Alice", DepartmentId = 1 },
new Employee { Id = 2, Name = "Bob", DepartmentId = 1 },
new Employee { Id = 3, Name = "Charlie", DepartmentId = 3 },
new Employee { Id = 4, Name = "David", DepartmentId = 1 },
new Employee { Id = 5, Name = "Eve", DepartmentId = 2 },
};
// 使用 Join() 方法连接两个集合
var query = departments.Join(
employees,
department => department.Id,
employee => employee.DepartmentId,
(department, employee) => new { Department = department.Name, Employee = employee.Name }
);
// 输出连接的结果
foreach (var item in query)
{
Console.WriteLine($"Department: {item.Department}, Employee: {item.Employee}");
}
运行以上代码后,输出结果如下:
Department: HR, Employee: Alice
Department: HR, Employee: Bob
Department: HR, Employee: David
Department: IT, Employee: Charlie
Department: Finance, Employee: Eve
以上代码创建了两个集合:departments
和 employees
,其中 Department
对象包含 Id
和 Name
属性,Employee
对象包含 Id
、Name
和 DepartmentId
属性。然后使用 Join()
方法连接这两个集合,按照 Department
对象的 Id
属性与 Employee
对象的 DepartmentId
属性进行匹配,返回结果包含了每个部门和该部门的所有员工的名称。
示例 2
下面是一个稍微复杂一些的示例,用于演示如何使用 Join()
方法连接三个集合,并且使用一个匿名类型把多个属性放在同一个对象内:
// 创建“外部的”集合
List<Department> departments = new List<Department>()
{
new Department { Id = 1, Name = "HR" },
new Department { Id = 2, Name = "Finance" },
new Department { Id = 3, Name = "IT" }
};
// 创建“中间的”集合
List<Position> positions = new List<Position>()
{
new Position { Id = 1, Name = "Manager", DepartmentId = 1 },
new Position { Id = 2, Name = "Specialist", DepartmentId = 2 },
new Position { Id = 3, Name = "Engineer", DepartmentId = 3 },
};
// 创建“内部的”集合
List<Employee> employees = new List<Employee>()
{
new Employee { Id = 1, Name = "Alice", PositionId = 1 },
new Employee { Id = 2, Name = "Bob", PositionId = 1 },
new Employee { Id = 3, Name = "Charlie", PositionId = 2 },
new Employee { Id = 4, Name = "David", PositionId = 3 },
new Employee { Id = 5, Name = "Eve", PositionId = 3 },
};
// 使用 Join() 方法连接三个集合,组合成一个匿名对象
var query = departments.Join(
positions,
department => department.Id,
position => position.DepartmentId,
(department, position) => new { Department = department.Name, Position = position.Name, PositionId = position.Id })
.Join(
employees,
x => x.PositionId,
employee => employee.PositionId,
(x, employee) => new { x.Department, x.Position, Employee = employee.Name }
);
// 输出连接的结果
foreach (var item in query)
{
Console.WriteLine($"Department: {item.Department}, Position: {item.Position}, Employee: {item.Employee}");
}
运行以上代码后,输出结果如下:
Department: HR, Position: Manager, Employee: Alice
Department: HR, Position: Manager, Employee: Bob
Department: Finance, Position: Specialist, Employee: Charlie
Department: IT, Position: Engineer, Employee: David
Department: IT, Position: Engineer, Employee: Eve
以上代码创建了三个集合:departments
、positions
和 employees
,其中 Department
对象包含 Id
和 Name
属性,Position
对象包含 Id
、Name
和 DepartmentId
属性,Employee
对象包含 Id
、Name
和 PositionId
属性。然后使用 Join()
方法连接这三个集合,按照 Department
对象的 Id
属性与 Position
对象的 DepartmentId
属性进行匹配,并且按照 Position
对象的 Id
属性与 Employee
对象的 PositionId
属性进行匹配,返回结果包含了每个部门、职位和该职位的员工的名称。