BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

LINQ
.NET 3.5+

LINQ Partitioning

The tenth part of the LINQ to Objects tutorial examines partitioning of collections using LINQ. Several standard query operators are provided that break a collection of data into two sections and return one of those parts as an enumerable object.

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:

var fruit = new string[]
    { "Apple", "Banana", "Cherry", "Damson", "Elderberry", "Grape", "Kiwi",
      "Lemon", "Melon", "Orange" };

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.

var partitioned = fruit.Take(5);

/* RESULTS

Apple
Banana
Cherry
Damson
Elderberry

*/

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.

var partitioned = fruit.Skip(5);

/* RESULTS

Grape
Kiwi
Lemon
Melon
Orange

*/

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.

int pageSize = 3;
int page = 2;
var partitioned = fruit.Skip((page - 1) * pageSize).Take(pageSize);

/* RESULTS

Damson
Elderberry
Grape

*/

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.

int pageSize = 3;
int page = 2;
var partitioned = fruit.TakeWhile(s => s.Length < 10);

/* RESULTS

Apple
Banana
Cherry
Damson

*/
6 September 2010