Đây là nội dung được chuyển từ: https://www.cnblogs.com/cncc/p/7985843.html
Phương thức Join trong LINQ được sử dụng để kết nối hai tập hợp dựa trên khóa khớp. Cú pháp của nó như sau:
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector
)
Tham số:
- outer: Dãy đầu tiên cần kết nối.
- inner: Dãy thứ hai cần kết nối với dãy đầu tiên.
- outerKeySelector: Hàm trích xuất khóa từ phần tử trong dãy đầu tiên.
- innerKeySelector: Hàm trích xuất khóa từ phần tử trong dãy thứ hai.
- resultSelector: Hàm tạo ra phần tử kết quả từ cặp phần tử trùng khớp.
Kiểu tham số:
- TOuter: Kiểu của phần tử trong dãy đầu tiên.
- TInner: Kiểu của phần tử trong dãy thứ hai.
- TKey: Kiểu của khóa được trả về bởi hàm chọn khóa.
- TResult: Kiểu của phần tử kết quả.
Xem thêm tại:
Ví dụ:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp33
{
class Program
{
static void Main(string[] args)
{
GroupJoinEx();
}
static void GroupJoinEx()
{
Person p1 = new Person() { Name = "ABC", Age = 18 };
Person p2 = new Person() { Name = "EFG", Age = 19 };
Person p3 = new Person() { Name = "LMN", Age = 20 };
Person p4 = new Person() { Name = "XYZ", Age = 21 };
List<Person> pList = new List<Person> { p1, p2, p3, p4 };
Department d1 = new Department() { Name = "A1", Employee = p1 };
Department d2 = new Department() { Name = "A2", Employee = p2 };
Department d3 = new Department() { Name = "A3", Employee = p1 };
Department d4 = new Department() { Name = "B1", Employee = p3 };
Department d5 = new Department() { Name = "B2", Employee = p4 };
Department d6 = new Department() { Name = "B3", Employee = p4 };
List<Department> dList = new List<Department> { d1, d2, d3, d4, d5, d6 };
var result = pList.Join(dList,
person => person,
department => department.Employee,
(person, department) => new
{
Person = person,
Department = department
});
foreach(var item1 in result)
{
Console.Write($"Name:{item1.Person} & Department:{item1.Department} ");
Console.WriteLine();
}
}
}
class Person
{
public string Name { set; get; }
public int Age { set; get; }
public override string ToString()
{
return $"{Name},{Age}";
}
}
class Department
{
public string Name { set; get; }
public Person Employee { set; get; }
public override string ToString()
{
return $"{Name}";
}
}
}
Kết quả đầu ra:
Phương thức GroupJoin tương tự như Join nhưng cho phép nhóm các phần tử từ dãy thứ hai theo khóa khớp. Cú pháp:
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, IEnumerable<TInner>, TResult> resultSelector
)
Tham số:
- outer: Dãy đầu tiên cần kết nối.
- inner: Dãy thứ hai cần kết nối với dãy đầu tiên.
- outerKeySelector: Hàm trích xuất khóa từ phần tử trong dãy đầu tiên.
- innerKeySelector: Hàm trích xuất khóa từ phần tử trong dãy thứ hai.
- resultSelector: Hàm tạo ra phần tử kết quả từ phần tử trong dãy đầu tiên và tập hợp các phần tử khớp trong dãy thứ hai.
Kiểu tham số:
- TOuter: Kiểu của phần tử trong dãy đầu tiên.
- TInner: Kiểu của phần tử trong dãy thứ hai.
- TKey: Kiểu của khóa được trả về bởi hàm chọn khóa.
- TResult: Kiểu của phần tử kết quả.
Xem thêm tại:
Ví dụ GroupJoin:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp33
{
class Program
{
static void Main(string[] args)
{
GroupJoinEx();
}
static void GroupJoinEx()
{
Person p1 = new Person() { Name = "ABC", Age = 18 };
Person p2 = new Person() { Name = "EFG", Age = 19 };
Person p3 = new Person() { Name = "LMN", Age = 20 };
Person p4 = new Person() { Name = "XYZ", Age = 21 };
List<Person> pList = new List<Person> { p1, p2, p3, p4 };
Department d1 = new Department() { Name = "A1", Employee = p1 };
Department d2 = new Department() { Name = "A2", Employee = p2 };
Department d3 = new Department() { Name = "A3", Employee = p1 };
Department d4 = new Department() { Name = "B1", Employee = p3 };
Department d5 = new Department() { Name = "B2", Employee = p4 };
Department d6 = new Department() { Name = "B3", Employee = p4 };
List<Department> dList = new List<Department> { d1, d2, d3, d4, d5, d6 };
var result = pList.GroupJoin(dList,
person => person,
department => department.Employee,
(person, departments) => new
{
Person = person,
Department = departments.Select(d => d)
});
foreach(var item1 in result)
{
Console.Write($"Name:{item1.Person} & ");
foreach(var item2 in item1.Department)
{
if(item1.Department.First() == item2)
Console.Write($"Department:{item2} ");
else
Console.Write($"{item2} ");
}
Console.WriteLine();
}
}
}
class Person
{
public string Name { set; get; }
public int Age { set; get; }
public override string ToString()
{
return $"{Name},{Age}";
}
}
class Department
{
public string Name { set; get; }
public Person Employee { set; get; }
public override string ToString()
{
return $"{Name}";
}
}
}
Kết quả đầu ra:
Chỉ khác biệt duy nhất giữa Join và GroupJoin nằm ở tham số cuối cùng. Như vậy, sự khác biệt chính giữa hai phương thức là: Join chỉ thực hiện liên kết đơn giản giữa hai tập hợp, trong khi GroupJoin sẽ nhóm các phần tử từ tập hợp thứ hai theo khóa khớp.