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

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.
- List<string> criketerLisr = new List<string>()
- { "Sachin", "Rahul", "Virat", "Rohit", "Dhawan", "Dhoni",
- "Viru", "Chetshwar", "Zaheer", "Bhuvneshwar" };
- Console.WriteLine("|============Skip Operator ============|");
- var resultSkip = criketerLisr.Skip(4);
- foreach (var item in resultSkip)
- {
- Console.WriteLine(item);
- }
- Console.WriteLine("|============SkipWhile Operator ============|");
- var resultSkipWhile = criketerLisr.SkipWhile(sw => sw.Length > 5);
- foreach (var item in resultSkipWhile)
- {
- Console.WriteLine(item);
- }

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.

- Console.WriteLine("|============Take Operator ============|");
- var resultTake = criketerList.Take(5);
- foreach (var item in resultTake)
- {
- Console.WriteLine(item);
- }

- Console.WriteLine("|============TakeWhile Operator ============|");
- var resultTakeWhile = criketerList.TakeWhile(x => x.Length > 4);
- foreach (var item in resultTakeWhile)
- {
- Console.WriteLine(item);
- }
- Console.WriteLine("|============Combining Skip and Take Operator ============|");
- var resultSkipAndTake = criketerList.Skip(3).Take(5);
- foreach (var item in resultSkipAndTake)
- {
- Console.WriteLine(item);
- }

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


Biswabid RathPosted Aug 17, 2018, 5:09 AM EDT
Nice article sir !! Keep it up ...
Hiten PandyaPosted Aug 16, 2018, 11:48 PM EDT
Good Explanation on Partitioning Operators. Thank you for sharing this article. Liked it.
Jitendra WaghalePosted Aug 16, 2018, 9:10 PM EDT
Nice Article!!