Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

LINQ Take and TakeWhile Partitioning operators in C#

In this article, I am going to explain you LINQ Take and Takewhile partitioning operators in C#. I have already explained Skip and SkipWhile operators in detail. Below is the four partitioning operators. Partitioning operators split the collection into two parts and returns one part only.
1) Skip
2) SkipWhile  (click here to read about Skip and SkipWhile)
3) Take - It takes element up-to specified position starting from first element in collection or sequence.
4) TakeWhile - It take elements from given collection until the specified condition is true.

Let's understand the these operators with examples.

Using Take Partitioning Operator In LINQ

Example 1

Let's apply Take operator on a string array which contains some words. Here Take operator will Take first 4 element from collection and return.


string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
var takeFirstFourWords = words.Take(4);
foreach (var word in takeFirstFourWords)
{
Console.WriteLine(word);
}
Console.ReadKey();
//OUTPUT:
//Five
//Four
//One
//Three

Example 2

Here, it will return first 5 numbers.


int[] numbers = { 9, 8, 6, 7, 2, 5, 4, 1, 3, 0 };
var takeFirstFiveNumbers = numbers.Take(5);
foreach (var number in takeFirstFiveNumbers)
{
Console.WriteLine(number);
}
Console.ReadKey();
//OUTPUT:
//9
//8
//6
//7
//2

Example 3

Take and TakeWhile operator do not support in query syntax but still we can use Take on query variable. Consider the example given below, we need 2 words from first 4 words whose Length is greater than 4.


string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };

var stringNumbers = from s in words
where s.Length > 4 // find words whose length is grater than 4
select s;

Console.WriteLine("words whose length is grater than 4");
foreach (var word in stringNumbers)
{
Console.WriteLine(word);
}
/*
OUTPUT:
Three
Eight
Seven
*/

var takeFirstTwoWords = stringNumbers.Take(2); //take first 2 words
Console.WriteLine("After taking two words");
foreach (var word in takeFirstTwoWords)
{
Console.WriteLine(word);
}
Console.ReadKey();
/*
OUTPUT:
Three
Eight
*/

Using TakeWhile Partitioning Operator In LINQ

Example 1

Let's understand TakeWhile operator with example. Consider the string array of words given below. Here first 3 elements are "Five", "Four", "One" and their length is less than 5. So, according to TakeWhile definition, for the first 3 elements condition is true because there length is less than 5, but from 4th element that is "Three" here length is equal to 5 that means if length is equal to 5 then condition becomes false, so TakeWhile will return those element for which condition was true.


string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
//length is 4 4 3 5 4 5 3 5 3 4
//length
var data = words.TakeWhile(x => x.Length foreach (var d in data)
{
Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//Five
//Four
//One

Example 2

Here, we will use same example as shown above, in above example we used ( less than operator, now we will use (>) greater than operator. Here the first element is "Five" whose length is equal to 4, but it is less than 5, so condition becomes false at first element only because we are checking for the element whose length is greater than 5. So, if condition becomes false at first statement only then it will return an empty collection.


string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
//length is 4 4 3 5 4 5 3 5 3 4
//length > 5 false
var data = words.TakeWhile(x => x.Length > 5);
foreach (var d in data)
{
Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//No Output

Example 3

Here, in below example first 5 elements when divided by 5, they have some Remainder but from 6th element that is '5' which has Remainder 0, here the condition becomes false because we are checking for Remainder should not be equal to zero, so it will return those element for which condition was true.


int[] numbers = { 9, 8, 6, 7, 2, 5, 4, 1, 3, 0 };

var data = numbers.TakeWhile(x => x % 5 != 0);
foreach (var d in data)
{
Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//9
//8
//6
//7
//2

Using TakeWhile With Index

We can use, second overload of TakeWhile. It passes index of each elements. In below example, it take all the element till the length of elements is greater than it's index. Here, condition becomes false at 5th element("Nine"), so it will return elements for which condition was true.


string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
// length is 4 4 3 5 4 5 3 5 3 4
// index is 0 1 2 3 4 5 6 7 8 9
// length > index true true true true false - - - - -
var data = words.TakeWhile((x,i) => x.Length > i);
foreach (var d in data)
{
Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//Five
//Four
//One
//Three


This post first appeared on ASPArticles, please read the originial post: here

Share the post

LINQ Take and TakeWhile Partitioning operators in C#

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×