📜  LINQ |分区运算符|跳过而

📅  最后修改于: 2021-05-29 14:17:50             🧑  作者: Mango

在LINQ中,分区运算符用于将给定序列分为两部分,而无需对元素进行排序,并返回其中一部分。标准查询运算符支持4种不同类型的分区运算符:

  1. 跳过
  2. 跳过而
  3. 边走边走

SkipWhile运算子

SkipWhile运算符用于基于谓词函数跳过元素,直到给定序列中的元素不满足给定条件,然后返回其余元素。此方法以两种不同的方式重载:

  • SkipWhile (IEnumerable ,Func ):只要指定条件为true,则此方法用于绕过给定序列中的元素,然后返回其余元素。
  • SkipWhile (IEnumerable ,Func ):只要指定条件为true,则此方法用于绕过给定序列中的元素,然后返回其余元素。元素的索引用于谓词函数的逻辑中。

重要事项:

  • 它不支持C#和VB.Net语言的查询语法。但是您可以使用SkipWhile方法查询变量,也可以将查询括在方括号中,然后调用SkipWhile方法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 它是通过使用延迟执行来实现的。
  • 如果源或条件为null,它将抛出ArgumentNullException。

范例1:

// C# program to illustrate the
// concept of SkipWhile operator
using System;
using System.Linq;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source
        int[] sequence = {45, 67, 89, 13,
                             56, 76, 67};
  
        // Query which skips the 
        // elements lesser than 67
        // Using SkipWhile method
        var result = sequence.OrderBy(s => s).SkipWhile(s => s < 67);
        Console.WriteLine("Sequence: ");
  
        // Display new sequence
        foreach(var val in result)
        {
            Console.WriteLine(val);
        }
    }
}
输出:
Sequence: 
67
67
76
89

范例2:

// C# program to find the names of the
// employees whose length is less than 4
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;} 
          
          
} 
      
public 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 = "Anu", emp_gender = "Female", 
                             emp_hire_date = "4/8/2017", emp_salary = 80000}, 
                                      
        new Employee(){emp_id = 213, emp_name = "Anil", emp_gender = "Male", 
                           emp_hire_date = "12/1/2016", emp_salary = 60000}, 
                              
        new Employee(){emp_id = 214, emp_name = "Anju", emp_gender = "Female", 
                             emp_hire_date = "17/6/2015", emp_salary = 50000}, 
    }; 
          
    // Query to find the names of the
    // employees whose length are less
    // than 4 Using SkipWhile method
    var res = emp.SkipWhile(e=> e.emp_name.Length > 4);
              
    foreach(var val in res) 
    { 
        Console.WriteLine("Employee Name: {0}",
                                 val.emp_name); 
    } 
      
    } 
} 
输出:
Employee Name: Anu
Employee Name: Anil
Employee Name: Anju