Thao tác trên các tập dữ liệu và trả về kết quả theo yêu cầu.
Toán tử thuộc nhóm này gồm:
Toán tử | Mô tả | Cú pháp C# | Cú pháp VB |
Distinct | Trả về tập dữ liệu chứa các giá trị không trùng nhau. | Chưa hỗ trợ | Distinct |
Except | So sánh giá trị của hai tập dữ liệu và trả về các giá trị từ một trong hai tập và không thuộc tập kia. | Chưa hỗ trợ | Chưa hỗ trợ |
Intersect | Trả về tập chứa các giá trị chung từ hai tập ban đầu. | Chưa hỗ trợ | Chưa hỗ trợ |
Union | Kết hợp hai tập dữ liệu và không lấy giá trị lặp lại. | Chưa hỗ trợ | Chưa hỗ trợ |
Ví dụ Distinct trong VB:
Dim classGrades = New System.Collections.Generic.List(Of Integer) From {63, 68, 71, 75, 68, 92, 75} Dim distinctQuery = From grade In classGrades Select grade Distinct Dim sb As New System.Text.StringBuilder("The distinct grades are: ") For Each number As Integer In distinctQuery sb.Append(number & " ") Next Console.Write(sb.ToString())
Kết quả:
Ví dụ Except – Enumerable.Except trong C#:
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 }; double[] numbers2 = { 2.2 }; IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2); foreach (double number in onlyInFirstSet) Console.WriteLine(number);
Ví dụ Except – Enumerable.Except trong VB:
Dim numbers1() As Double = {2.0, 2.1, 2.2, 2.3, 2.4, 2.5} Dim numbers2() As Double = {2.2} Dim onlyInFirstSet As IEnumerable(Of Double) = numbers1.Except(numbers2) Dim output As New System.Text.StringBuilder For Each number As Double In onlyInFirstSet output.AppendLine(number) Console.WriteLine(number) Next
Kết quả:
Ví dụ Intersect – Enumerable.Intersect trong C#:
int[] id1 = { 44, 26, 92, 30, 71, 38 }; int[] id2 = { 39, 59, 83, 47, 26, 4, 30 }; IEnumerable<int> both = id1.Intersect(id2); foreach (int id in both) Console.WriteLine(id);
Ví dụ Intersect – Enumerable.Intersect trong VB:
Dim id1() As Integer = {44, 26, 92, 30, 71, 38} Dim id2() As Integer = {39, 59, 83, 47, 26, 4, 30} Dim intersection As IEnumerable(Of Integer) = id1.Intersect(id2) Dim output As New System.Text.StringBuilder For Each id As Integer In intersection output.AppendLine(id) Console.WriteLine(id) Next
Kết quả:
Ví dụ Union – Enumerable.Union trong C#:
int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 }; int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 }; IEnumerable<int> union = ints1.Union(ints2); foreach (int num in union) { Console.Write("{0} ", num); Console.Write("\n"); }
Ví dụ Union – Enumerable.Union trong VB
Dim ints1() As Integer = {5, 3, 9, 7, 5, 9, 3, 7} Dim ints2() As Integer = {8, 3, 6, 4, 4, 9, 1, 0} Dim union As IEnumerable(Of Integer) = ints1.Union(ints2) Dim output As New System.Text.StringBuilder For Each num As Integer In union output.AppendLine(num & " ") Console.WriteLine(num & " ") Next
Kết quả:
Ý kiến bài viết