LINQ | Partition Operator | SkipWhile
In LINQ, partition operators are used for separating the given sequence into two portions without sorting the elements and return one of the portions. The Standard Query Operators supports 4 different types of partition operators:
- Skip
- SkipWhile
- Take
- TakeWhile
SkipWhile Operator
The SkipWhile operator is used for skipping the elements based on the predicate function until the element in the given sequence does not satisfy the given condition and then return the remaining elements. This method is overloaded in two different ways:
- SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>): This method is used to bypass the elements in a given sequence as long as a specified condition is true and then returns the remaining elements.
- SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>): This method is used to bypass the elements in a given sequence as long as a specified condition is true and then returns the remaining elements. The index of the element is used in the logic of the predicate function.
Important Points:
- It does not support query syntax in C# and VB.Net languages. But you can use the SkipWhile method to query variable or you can wrap your query in brackets and then the call SkipWhile method.
- It support method syntax in both C# and VB.Net languages.
- It present in both the Queryable and Enumerable class.
- It is implemented by using deferred execution.
- It will throw an ArgumentNullException if the source or the condition is null.
Example 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); } } } |
Output:
Sequence: 67 67 76 89
Example 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<Employee> emp = new List<Employee>(){ 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); } } } |
Output:
Employee Name: Anu Employee Name: Anil Employee Name: Anju
Please Login to comment...