📜  LINQ |发电运营商| DefaultIfEmpty

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

生成运算符用于创建新的值序列。标准查询运算符支持4种不同类型的生成运算符:

  1. DefaultIfEmpty
  2. 空的
  3. 范围
  4. 重复

DefaultIfEmpty运算符

DefaultIfEmpty运算符用于将空集合或序列替换为默认值的单例集合或序列。换句话说,如果源为空,它将返回具有默认值的集合或序列,否则返回源。

  • 此运算符以两种不同的方式重载:
    • DefaultIfEmpty (IEnumerable ,TSource):此方法用于返回指定序列的元素,如果序列为空,则返回单例集合中的指定值。
    • DefaultIfEmpty (IEnumerable ):此方法用于返回指定序列的元素,或者如果序列为空,则返回单例集合中类型参数的默认值。
  • 它不支持C#和VB.Net语言的查询语法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 它是通过使用延迟执行来实现的。
  • 如果给定源为null,则DefaultIfEmpty (IEnumerable )将返回ArgumentNullException
  • 引用类型和可为空的类型的默认值为null。

范例1:

// C# program to illustrate the
// use of DefaultIfEmpty operator
using System;
using System.Linq;
using System.Collections.Generic;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source 1
        int[] sequence1 = {};
  
        // The sequence is empty so it 
        // will return the default value
        // Using DefaultIfEmpty
        foreach(var val1 in sequence1.DefaultIfEmpty())
        {
            Console.WriteLine(val1);
        }
  
        // Data source 2
        string[] sequence2 = {"Geek", "Geeks123",
                                "GeeksforGeeks"};
  
        // The given sequence 2 is non-empty so 
        // it will return the sequence
        // Using DefaultIfEmpty
        foreach(var val2 in sequence2.DefaultIfEmpty())
        {
            Console.WriteLine(val2);
        }
    }
}
输出:
0
Geek
Geeks123
GeeksforGeeks

范例2:

// C# program to illustrate the 
// use of DefaultIfEmpty operator
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},
        };
  
        // Using DefaultIfEmpty operator
        foreach(Employee e in emp.DefaultIfEmpty())
        {
            Console.WriteLine(e.emp_name);
        }
    }
}
输出:
Anjita
Soniya
Rohit
Supriya
Anil
Anju