In Focus

LINQ Extension Methods - Partitioning Operator - Part Four

This article is continuation of Linq extension methods. In this article I am going to demostrate partitionaning operators like skip, skipWhile, take and TakeWhile.

This article is in continuation of my LINQ Extension Methods Series. In this article, I am going to demonstrate the partitioning operators, like Skip, skipWhile, Take, and TakeWhile.

If you have missed the earlier articles of this series, please click the below links and have a look at other operators.

  • Projection, Filter, And Sorting Operator - Part One  Click Here
  • Quantifier And Aggregation Operator - Part Two Click Here
  • Element Operator And Set Operator - Part Three Click Here 
LINQ Extension Methods - Partitioning Operator

Partitioning operators are used for splitting a collection into two parts and returning one of the parts. Let's start with the Skip and SkipWhile operators. To demonstrate these operators, I have chosen cricketerlist collection which contains a list of cricketers played for India.

 LINQ Extension Methods - Partitioning Operator 
  1. List<string> criketerLisr = new List<string>() 
  2.                            { "Sachin""Rahul""Virat""Rohit""Dhawan""Dhoni"
  3.                              "Viru""Chetshwar""Zaheer""Bhuvneshwar" };  
  4.              
  5.  Console.WriteLine("|============Skip Operator ============|");  
  6.  var resultSkip = criketerLisr.Skip(4);  
  7.      foreach (var item in resultSkip)  
  8.      {  
  9.        Console.WriteLine(item);     
  10.      }  
  11.   
  12.  Console.WriteLine("|============SkipWhile Operator ============|");  
  13.  var resultSkipWhile = criketerLisr.SkipWhile(sw => sw.Length > 5);  
  14.      foreach (var item in resultSkipWhile)  
  15.      {  
  16.        Console.WriteLine(item);  
  17.      }  
 LINQ Extension Methods - Partitioning Operator
 
One point to note here is that Skip and SkipWhile will work with only method syntax. They are not valid in query syntax. In the above example, I have used Skip with 4, so then it skips the first 4 elements from the collection and you see the result in output window - It returns from the 5th element. Here, the count starts from position 1, not from 0.

For SkipWhile, I have given a condition for the elements which have the name length more than 4. So, it skipped the first few elements because it is satisfied with the length of more than 4 characters and returns from "Viru" because it has a length equal to 4 but the condition has failed. In SkipWhile, once the condtion is failed, it will return the remaining elements as colletion. 
LINQ Extension Methods - Partitioning Operator
  1. Console.WriteLine("|============Take Operator ============|");  
  2.   
  3.            var resultTake = criketerList.Take(5);  
  4.   
  5.            foreach (var item in resultTake)  
  6.            {  
  7.                Console.WriteLine(item);  
  8.            }  
 LINQ Extension Methods - Partitioning Operator

In the above example, I have used take(5) which means that the take method will pick the first 5 element from the collection. Take method is used for picking n number of elements from a collection.  
  1. Console.WriteLine("|============TakeWhile Operator ============|");  
  2.   
  3.            var resultTakeWhile = criketerList.TakeWhile(x => x.Length > 4);  
  4.   
  5.            foreach (var item in resultTakeWhile)  
  6.            {  
  7.                Console.WriteLine(item);  
  8.            }  
LINQ Extension Methods - Partitioning Operator 
 
In the above example, I have used TakeWhile method to return the elements from collection until a specified condition has failed. Here, the condition on the name length is more than 4, it failed at the name "Viru". So, it will not come in our output window. We are able to see the name of cricketers up to "Dhoni"  because up to that our given condition satisfies.
  1. Console.WriteLine("|============Combining Skip and Take Operator ============|");  
  2.   
  3.             var resultSkipAndTake = criketerList.Skip(3).Take(5);  
  4.   
  5.             foreach (var item in resultSkipAndTake)  
  6.             {  
  7.                 Console.WriteLine(item);  
  8.             }  

LINQ Extension Methods - Partitioning Operator

 

In the above example, I have used a combination of Skip and Take operators together, so it will execute from left to right. First, the Skip method will apply collection and then take method on that result. The skip(3).Take(5) means the first 3 elements are omitted from the collection and the take method picks the next 5 members from the collection. The first three (Sachin, Rahul, and Virat) are skipped by the Skip method and the next 5 in the above result are picked by take method.

Conclusion

Here, I have explained the Skip, SkipWhile, Take, and Takewhile operators with a small practical example. Skip and Take are the most powerful methods in LINQ and can be use to implement paging in an easy way.

Thank you for your valuable time. Hope you have enjoyed reading.