Thực hiện thao tác giúp cho việc theo dõi mối quan hệ giữa các nguồn dữ liệu được thuận tiện, dễ dàng – điều không thể thực hiện được khi theo dõi trực tiếp từ các nguồn dữ liệu.
Các toán tử thuộc nhóm kết nối gồm:
Toán tử | Mô tả | Cú pháp C# | Cú pháp VB |
Join | Kết nối hai nguồn dữ liệu dựa trên các khoá so khớp. | join…in…on… equals… | From x In
…, y In … Where x.a = y.a |
GroupJoin | Kết nối và nhóm các nguồn dữ liệu theo các phần tử so khớp. | join … in … on …
equals … into … |
Group Join
… In … On … |
Ví dụ chúng ta có hai bảng dữ liệu (hay hai đối tượng) là phòng ban (department) và nhân viên (employee). Mỗi phòng ban có thể có nhiều nhân viên và mỗi nhân viên chỉ thuộc về một phòng ban. Định nghĩa hai lớp Department và Employee như sau:
Mã C# hoàn chỉnh (trong Visual Studio):
using System; using System.Collections.Generic; using System.Linq; namespace Operators { class JoinTables { class DepartmentClass { public int DepartmentId { get; set; } public string Name { get; set; } } class EmployeeClass { public int EmployeeId { get; set; } public string EmployeeName { get; set; } public int DepartmentId { get; set; } } static void Main(string[] args) { List <DepartmentClass> departments = new List<DepartmentClass>(); departments.Add(new DepartmentClass { DepartmentId = 1, Name = "Account" }); departments.Add(new DepartmentClass { DepartmentId = 2, Name = "Sales" }); departments.Add(new DepartmentClass { DepartmentId = 3, Name = "Marketing" }); List <EmployeeClass> employees = new List <EmployeeClass>(); employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 1,EmployeeName = "William" }); employees.Add(new EmployeeClass { DepartmentId = 2, EmployeeId = 2, EmployeeName = "Miley" }); employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 3, EmployeeName = "Benjamin" }); var list = (from e in employees join d in departments on e.DepartmentId equals d.DepartmentId select new { EmployeeName = e.EmployeeName, DepartmentName = d.Name }); foreach (var e in list) { Console.WriteLine("Employee Name = {0} , Department Name = {1}", e.EmployeeName, e.DepartmentName); } Console.WriteLine("\nPress any key to continue."); Console.ReadKey(); } } }
Mã VB hoàn chỉnh (trong Visual Studio)
Module Module1 Sub Main() Dim account As New Department With {.Name = "Account", .DepartmentId = 1} Dim sales As New Department With {.Name = "Sales", .DepartmentId = 2} Dim marketing As New Department With {.Name = "Marketing", .DepartmentId = 3} Dim departments As New System.Collections.Generic.List(Of Department) (New Department() {account, sales, marketing}) Dim william As New Employee With {.EmployeeName = "William", .EmployeeId = 1, .DepartmentId = 1} Dim miley As New Employee With {.EmployeeName = "Miley", .EmployeeId = 2, .DepartmentId = 2} Dim benjamin As New Employee With {.EmployeeName = "Benjamin", .EmployeeId = 3, .DepartmentId = 1} Dim employees As New System.Collections.Generic.List(Of Employee)( New Employee() {william, miley, benjamin}) Dim list = (From e In employees Join d In departments On e.DepartmentId Equals d.DepartmentId Select New Person With {.EmployeeName = e.EmployeeName, .DepartmentName = d.Name}) For Each e In list Console.WriteLine("Employee Name = {0} , Department Name = {1}", e.EmployeeName, e.DepartmentName) Next Console.WriteLine(vbLf &"Press any key to continue.") Console.ReadKey() End Sub Class Employee Public Property EmployeeId As Integer Public Property EmployeeName As String Public Property DepartmentId As Integer End Class Class Department Public Property Name As String Public Property DepartmentId As Integer End Class Class Person Public Property EmployeeName As String Public Property DepartmentName As String End Class End Module
Mã C# với LINQPad
Mã VB với LINQPad
Kết quả:
Ý kiến bài viết