📜  LINQ |发电运营商|空,范围和重复

📅  最后修改于: 2021-05-29 22:15:31             🧑  作者: Mango

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

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

空运算符

Empty运算符用于返回空集合。换句话说,我们可以说它返回一个空的IEnumerable ,其中包含指定的类型参数。

  • 它不支持C#和VB.Net语言的查询语法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它仅存在于Enumerable类中。

例子:

// C# program to illustrate the
// concept of Empty operator
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Query which return empty collection
        // Using Empty method
        var res = Enumerable.Empty();
  
        // Display the total number of elements
        // present in the given collection
        Console.WriteLine("How many elements present"+
              "in the collection?: {0}", res.Count());
  
        // Display the type in the given collection
        Console.WriteLine("Type is: {0}", res.GetType().Name);
    }
}
输出:
How many elements presentin the collection?: 0
Type is: String[]

范围运算符

Range运算符用于产生包含数字序列的集合。换句话说,它用于返回IEnumerable 类型的集合,该集合具有给定范围内的整数序列。

  • 它不支持C#和VB.Net语言的查询语法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它仅存在于Enumerable类中。
  • 如果count的值小于零或start + count-1大于MaxValue,则它将引发ArgumentOutOfRangeException。
  • 它是通过使用延迟执行来实现的。

例子 :

// C# program to illustrate the
// concept of Range operator
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Query which provides 10 elements
        // starting from 100
        // Using Range method
        var res = Enumerable.Range(100, 10);
  
        // Display elements
        Console.WriteLine("Elements are:");
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
  
        // Display the total number of elements
        // present in the given collection
        Console.WriteLine("How many elements present"+
             " in the collection?: {0}", res.Count());
    }
}
输出:
Elements are:
100
101
102
103
104
105
106
107
108
109
How many elements present in the collection?: 10

重复运算符

重复运算符用于创建保存一个重复值的集合。换句话说,可以说它用于创建具有重复项数的IEnumerable 类型。

  • 它不支持C#和VB.Net语言的查询语法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它仅存在于Enumerable类中。
  • 如果count的值小于零,它将抛出ArgumentOutOfRangeException。
  • 它是通过使用延迟执行来实现的。

例子:

// C# program to illustrate the 
// concept of Repeat operator
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Query which repeats a string
        // 10 times
        // Using Range method
        var res = Enumerable.Repeat("Welcome to GeeksforGeeks", 10);
  
        // Display repeated collection
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
  
        // Display the total times 
        // the element repeat
        Console.WriteLine("How many times the string"+
                  " repeat?: {0} times", res.Count());
    }
}
输出:
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
Welcome to GeeksforGeeks
How many times the string repeat?: 10 times