LINQ 中的集合运算



共有四个集合运算符,每个运算符根据不同的条件产生结果。

运算符 描述 C# 查询表达式语法 VB 查询表达式语法
Distinct(去重) 在集合中过滤重复数据,返回唯一值列表。 不适用 Distinct(去重)
Except(剔除) 比较两个集合中的值,返回第一个集合中,而不在另一个集合中的值。 不适用 不适用
Intersect(交集) 返回在两个独立集合中找到的值集合,即相同的值。 不适用 不适用
Union(并集) 将两个不同集合的内容组合到单个列表中,并且不包含任何重复内容。 不适用 不适用

Distinct 示例 - 查询表达式

VB

Module Module1

   Sub Main()
  
      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

      MsgBox(sb.ToString())
	  
   End Sub
   
End Module

在编译并执行以上代码后,将产生以下结果:

The distinct grades are: 63 68 71 75 92

Except 示例 - Enumerable.Except 方法

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operators {
   class Program {
      static void Main(string[] args) {
      
         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);
            Console.ReadLine();
      }
   }
}

VB

Module Module1

   Sub Main()
   
      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
	  
      Console.ReadLine()
     
   End Sub
   
End Module

在编译并执行以上的 C# 或 VB 代码后,将产生以下结果:

2
2.1
2.3
2.4
2.5

Intersect 示例 - Enumerable.Intersect 方法

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operators {
   class Program {
      static void Main(string[] args) {

         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);
            Console.ReadLine();
      }
   }
}	

VB

Module Module1

   Sub Main()
   
      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
     
      Console.ReadLine()
     
   End Sub
   
End Module

在编译并执行以上的 C# 或 VB 代码后,将产生以下结果:

26
30

Union 示例 - Enumerable.Union 方法

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operators {
   class Program {
      static void Main(string[] args) {
      
         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");
         }
			
         Console.ReadLine();
      }
   }
}

VB

Module Module1

   Sub Main()
   
      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
     
      Console.ReadLine()
	  
   End Sub
   
End Module

在编译并执行以上的 C# 或 VB 代码后,将产生以下结果:

5
3 
9 
7 
8 
6 
4 
1 
0
linq_query_operators.htm
广告