📜  LINQ |设置运算符|清楚的

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

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

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

区别算子

Distinct运算符返回不包含重复值的集合。换句话说,我们可以说此运算符从集合或集合中删除所有重复的值,并返回包含唯一或不相似值的集合或集合。

  • 它不支持C#和VB.Net语言的查询语法。但是您可以对查询变量使用Distinct方法,也可以将查询括在方括号中,然后调用Distinct方法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 它是通过使用延迟执行来实现的。
  • 使用复杂类型的集合时,必须使用IEqualityComparer接口使用Distinct方法,因为Distinct方法不会比较复杂类型的值。

范例1:

// C# program to find the sequence 
// which contains unique elements
using System;
using System.Linq;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source
        char[] sequence = {'m', 'q', 'o', 's', 'y', 
                          'a', 'a', 'c', 'y', 'o'};
  
        // Display the sequence
        Console.WriteLine("Sequence is: ");
  
        foreach(var s in sequence)
        {
            Console.WriteLine(s);
        }
  
        // Get the sequence which contains unique 
        // elements Using Distinct function
        var result = sequence.Distinct();
  
        Console.WriteLine("New Sequence: ");
  
        foreach(var val in result)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Sequence is: 
m
q
o
s
y
a
a
c
y
o
New Sequence: 
m
q
o
s
y
a
c

范例2:

// C# program to find the salary of 
// the employees with dissimilar values
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List emp = new List() {
              
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
                                      
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
                                      
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
                                    
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
                                        
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
                                  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female", 
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to find the salary of the
        // employees with dissimilar values
        // Using Distinct method
        var res = emp.Select(e => e.emp_salary).Distinct();
          
        Console.WriteLine("Employee Salary: ");
          
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Employee Salary: 
20000
30000
40000
50000