📜  LINQ |设置运算符|联盟

📅  最后修改于: 2021-05-29 18:16:24             🧑  作者: Mango

在LINQ中,Set运算符是查询表达式中的那些运算符,它们返回结果集,该结果集基于相同或不同集合或序列或集中同等元素的存在或不存在。标准查询运算符包含以下集合运算符:

  1. 联盟
  2. 相交
  3. 除了
  4. 清楚的

联盟

Union运算符返回设置的Union。换句话说,我们可以说它返回的集合包含了出现在给定输入集合或序列中的唯一元素。

  • 它不支持C#和VB.Net语言的查询语法。但是您可以对查询变量使用Union方法,也可以将查询括在方括号中,然后调用Union方法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 它是通过使用延迟执行来实现的。
  • 当您处理复杂类型的集合时,必须使用IEqualityComparer接口,否则,Union方法将给您不正确的结果。

范例1:

// C# program to find the union 
// of the given sequence
using System;
using System.Linq;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source
        char[] sequence1 = {'m', 'q', 'o', 's', 'y', 'a'};
        char[] sequence2 = {'p', 't', 'r', 's', 'w', 'z'};
  
        // Display the sequences
        Console.WriteLine("Sequence 1 is: ");
  
        foreach(var s1 in sequence1)
        {
            Console.WriteLine(s1);
        }
  
        Console.WriteLine("Sequence 2 is: ");
  
        foreach(var s2 in sequence2)
        {
            Console.WriteLine(s2);
        }
  
        // Get union of the given sequence
        // Using Union function
        var result = sequence1.Union(sequence2);
  
        Console.WriteLine("Unique Sequence: ");
  
        foreach(var val in result)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Sequence 1 is: 
m
q
o
s
y
a
Sequence 2 is: 
p
t
r
s
w
z
Unique Sequence: 
m
q
o
s
y
a
p
t
r
w
z

范例2:

// C# program to find the common 
// languages known by the employees
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee1 {
  
    public int emp_id1
    {
        get;
        set;
    }
  
    public string emp_name1
    {
        get;
        set;
    }
    public string emp_lang1
    {
        get;
        set;
    }
}
// Employee details
public class Employee2 {
  
    public int emp_id2
    {
        get;
        set;
    }
  
    public string emp_name2
    {
        get;
        set;
    }
    public string emp_lang2
    {
        get;
        set;
    }
}
public class GFG {
  
    // Main method
    static public void Main()
    {
        List emp1 = new List() {
  
            new Employee1() {emp_id1 = 209, emp_name1 = "Anjita",
                                               emp_lang1 = "C#"},
  
            new Employee1() {emp_id1 = 210, emp_name1 = "Soniya",
                                                emp_lang1 = "C"},
  
            new Employee1() {emp_id1 = 211, emp_name1 = "Rohit",
                                            emp_lang1 = "Java"},
  
        };
  
        List emp2 = new List() {
  
            new Employee2() {emp_id2 = 290, emp_name2 = "Anjita",
                                               emp_lang2 = "C#"},
  
            new Employee2() {emp_id2 = 212, emp_name2 = "MaMa", 
                                         emp_lang2 = "Python"},
  
            new Employee2() {emp_id2 = 233, emp_name2 = "Rima", 
                                           emp_lang2 = "Java"},
  
        };
  
        // Query to find the common languages
        // known by the employees
        // Using Union method
        var res = emp1.Select(e => e.emp_lang1).Union(emp2.Select(e => e.emp_lang2));
          
        Console.WriteLine("Common Languages: ");
          
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Common Languages: 
C#
C
Java
Python