Prologue:
In one of my previous articles, we discussed the use of Any, All and Aggregate methods in LINQ. As the continuation of that article, today we shall see about the Take/TakeWhile and Skip/SkipWhile operators and their usage.
Short intro about LINQ:
LINQ is a programming model that introduces queries as a first-class concept into any Microsoft .NET language. However, complete support for LINQ requires some extensions in the language used. These extensions boost productivity, thereby providing a shorter, more meaningful, and expressive syntax to manipulate data.
Take, Skip, TakeWhile and SkipWhile are all called Partitioning Operators since they obtain a section of an input sequence as determined by a given condition.
Let us discuss these operators.
Take method:
Signature of Take ():
The Take (int...) method of System.Linq.Queryable class returns the portion of a source i.e. an IEnumerable collection of a source with the first n number of items from the source.
Example:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.Take(5);
In the above example the Take method returns an IEnumerable collection with only the first 5 items as we have specified in the Take argument.
I.e. it will return 5, 4, 1, 3 and 9 only.
TakeWhile method:
Signature of TakeWhile ():
Another signature for overriding the method:
The TakeWhile (Func...) will return elements starting from the beginning of an IEnumerable collection until it satisfies the condition specified. If the condition is false then it will return the collection immediately.
Example I:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 9);
In the above example the TakeWhile will return the collection of items when the condition fails. i.e. if the control reaches the item 9 then the condition fails here so it will return the items from 5 to 3 [5, 4, 1, 3].
Quick Note:
Even though the collection has the items which are less than 9 such as 8,6,7,2,and 0 but it returned only 5,4,1 and 3. The point here is the TakeWhile won't consider the items which are the item which makes the condition fail.
Example II:
String colection = { "one", "two", "three", "four", "five" };
var strings = collection.TakeWhile(n => n.Length < 4);
In this example, the TakeWhile will return the string collection having a length of 4 characters i.e. it will return only the "one" and "two" because the length of the "three" is 5 so it fails there.
Example III:
String colection = { "one", "two", "three", "four", "five" };
var strings = collection.TakeWhile((s, index)=> s.Length > index );
In this example the TakeWhile will return the items starting from the beginning of the array until a string length is greater than it's position in the collection.
So this example will return "one" and "two". Because the "three" has the length of 5 and its position [index] is 2. Since 2 is less than 5 the condition fails at "three".
Skip method:
Signature of Skip ():
The Skip (int...) method of the System.Linq.Queryable class will skip the n number of items from the starting position and will return the remaining items as an IEnumerable collection.
Example:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.Skip(3);
In this example, the Skip method will skip the first 3 items from the collection and return the remaining items as an IEnumerable collection.
So the output of this example will be 3, 9, 8, 6, 7, 2 and 0.
SkipWhile method:
Signature of SkipWhile ():
Another signature for overriding:
The SkipWhile method will skip the first n number of items from the IEnumerable collection and return the remaining items.
Example I:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7 };
var remaining = numbers.SkipWhile(n => n < 9);
In this example the SkipWhile operator will skip the items from the starting position until the conditions fails. Once the condition has failed then it will return the remaining items as an IEnumerable collection.
So the output for this example is 9, 8, 6 and 7.
Example II:
int[] numbers = { 5, 4, 1, 3, 9, 8, 2, 0 };
var indexed = numbers.SkipWhile((n, index) => n > index);
In this example the SkipWhile method will skip the items greater than their positions.
So in this example the output is 1, 3, 9, 8, 2 and 0. Because the position of the 1 is 2 so here the condition fails.
Combining Take and Skip:
We can use these Take and Skip in a single statement.
Example:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.Take(5).Skip(4);
If we need to get the value of the 5th item then we can use the Take and Skip like in the above.
This example will take the first 5 items, skip the first 4 of that and return the single remaining element.
Summary:
In this article, we have seen the basics of Take, Skip, TakeWhile and SkipWhile.
Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which enable me to give a better article the next time.
Please rate this Article.
Thanks.