Skip Navigation LinksHome > Categories > Code from a Category

LINQ Partitioning



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 1002
Add Date: 12/11/2012
In this article we will learn about LINQ Partitioning

Partitioning
Partitioning in LINQ is the process of breaking an enumerable sequence into two parts and retrieving one of those parts without altering the order of the items. A common use for partitioning is to break a collection into a number of pages and displaying or processing one page at a time.

LINQ provides two standard query operators that help in this task by either obtaining the items from the start of a collection or skipping those items. In addition, two further standard query operators are available that either retrieve items in the original order whilst a condition is met, or skip items using a predicate. In this article we will review these four methods.

Partitioning Standard Query Operators

To demonstrate the partitioning operators we need some sample data. For this article we will use a simple array of strings:

Take

The first of the partitioning operators that we will consider is Take. This operator returns a number of items from the beginning of the source collection. The number is specified as the only parameter to the method. In the sample code below, the first five elements of the array of fruit names are extracted.

Skip

The Skip method returns all of the items that the Take extension method would not return when used with the same argument. In the case of Skip, a number of items from the start of the source sequence are ignored and the remaining items are returned. In the following example, the first five elements of the string array are skipped and the remaining items are included in the results.

When used together, the Skip and Take methods can be used to obtain "pages" of items from the source collection. First you skip the correct number of items to reach the start of the desired page. Next, you use the Take method to obtain the number of items that should appear on a page. You can see this in the next example. If you change the value of the page variable you can retrieve pages of information from the source array.

TakeWhile

The third partition operator is TakeWhile. As the name may suggest, this extension method retrieves items from the start of a sequence. Instead of specifying a fixed number of results, a predicate is provided using a Func delegate. This condition is evaluated for each item in the collection until the first time that it returns false. The items up to but not including the false result are returned.

The following sample code retrieves items from the start of the array until a string with a length of ten or more characters is encountered. This condition is specified using a lambda expression. Even though further items exist in the array that pass the condition, these are not returned.

SkipWhile

SkipWhile is the opposite of TakeWhile. Again a predicate is specified but this time items that meet the condition are skipped. When an item is encountered that causes the predicate to return false, this item and all that follow it are returned.

Partitioning with Query Expression Syntax

You can use partitioning with queries written using query expression syntax. However, there are no specific clauses for partitioning. Instead, you can apply the standard query operators to the results of a query. As the query and the partition operators will generally use deferred execution, this has no impact on performance or memory use.


Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.