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