Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to use Take/TakeWhile and Skip/SkipWhile Operators in LINQ

Author: Rudy Pinson

In this article, you will learn how to use Take/TakeWhile and Skip/SkipWhile operators of LINQ in ASP.NET.

 

This article is the continuation of previous articles on DevASP.NET about LINQ. You can refer to these articles on DevASP.NET to read these articles and learn more about LINQ.

Take/TakeWhile and Skip/SkipWhile are called partitioning operators in LINQ. It returns a part of collection elements and leave the rest. These operators are used to create sub-collections from large collections.

 

Take
 
Take operator returns sequence of first “n” elements from collection where “n” is the number of elements that we provide in Take operator.
 
Example:
 
C#
 
int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 0, 5 };
numbers.Take(4);
 
VB.NET
 
Dim numbers As Integer() = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5}
numbers.Take(4)
 
We have declared array of integers. Then operator will return first four numbers from integer array and will not consider any other. The result will be:
8
3
6
9
 
TakeWhile
 
TakeWhile operator returns the sequence of elements starting from the beginning of array until the mentioned condition is false.
 
Example:
 
C#
 
int[] numbers = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5 };
numbers.TakeWhile(x => x < 6);
 
VB.NET
 
Dim numbers As Integer() = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5}
numbers.TakeWhile(Function(x) x < 6)
 
We are using same array of integer. TakeWhile operator will return until the condition mentioned is true. The result will be:
1
3
 
Here you can see there are other items in the collection which are less than six but TakeWhile operator will not consider those because condition fails on the third item.
 
Another Example:
 
C#
 
string[] strings = { "VB", "CSharp", "JAVA", "PHP" };
strings.TakeWhile(x => x.Length < 5);
 
VB.NET
 
Dim strings As String() = {"VB", "CSharp", "JAVA", "PHP"}
strings.TakeWhile(Function(x) x.Length < 5)
 
Here we have declared array of strings. TakeWhile operator will return elements that have length of characters less than 5. Although there are other items which have length less than 5 but the result will be only the first item because the condition will false on second element. The result will be:
VB
 
Skip
 
Skip operator is said to be reverse of Take operator because it skips the “n” number of mentioned elements and returns remaining elements.
 
Example:
 
C#
 
int[] numbers = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5 };
numbers.Skip(7);
 
VB.NET
 
Dim numbers As Integer() = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5}
numbers.Skip(7)
 
Here, Skip operator will skip first 7 numbers and return remaining numbers. The result will be:
7
0
5
 
SkipWhile
 
SkipWhile operator is the reverse of TakeWhile operator. SkipWhile operator skips the number of elements in collection until the condition is true.
 
Example:
 
C#
 
int[] numbers = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5 };
numbers.SkipWhile(x => x < 7);
 
VB.NET
Dim numbers As Integer() = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5}
numbers.SkipWhile(Function(x) x > 2)
 
Here, SkipWhile operator will skip all elements appear before seven and returns elements less than seven after 7. The result will be:
0
5
 
Both Take and Skip Operators
 
You can use both Take and Skip Operators in one statement if needed. If we want to get the 9th element from our array of integer then we can use both.
 
Example:
 
C#
 
int[] numbers = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5 };
numbers.Take(9).Skip(8);
 
VB.NET
 
Dim numbers As Integer() = {1, 3, 6, 9, 4, 2, 8, 7, 0, 5}
numbers.Take(9).Skip(8)
 
Here, Take operator takes the first nine elements and skip operator will skip the eight elements from that nine and returns the 9th one. The result will be:
0

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use LINQ to find selected items from Listbox in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net