LINQ Partitioning operators in ASP.NET

April 14, 2014 0 Comments


The Partitioning operators in LINQ are used to divide an input sequence into two sections, without rearranging the elements, and then returning the result set with one of the sections that satisfies the given condition. The Take, Skip, TakeWhile, and SkipWhile clauses are referred to as the Partitioning operators. The Take clause takes the elements up to a specified position in a sequence. The TakeWhile clause takes the elements based on the specified function until an element does not satisfy the given condition.
The syntax of the Take clause is:
For C#
public static IEnumerabIe<T> Take<T>( this IEnumerabIe<T> source, int count);
The syntax of the Skip clause is:
For C#
public static IEnumerable<T> Skip<T>( this IEnumerab!e<T> source, int count);

Let's take an simple example
        <asp:ListBox ID="ListBox1" runat="server" Height="134px" Width="197px">
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text=" Partitioning Operator Linq" />

Code Behind code

protected void Button1_Click(object sender, EventArgs e)
        string[] fruits = { "apple", "banana", "pineapple", "papaya", "blueberry", "cheery" };
        int size = 5;
        int pages = (int)Math.Ceiling(fruits.Count() / (double)size);
        for (int page = 0; page <pages; page++)
            ListBox1.Items.Add("page " + page + ":");
            var names = (from p in fruits
                         orderby p descending
                         select p).Skip(page * size).Take(size);
            foreach (var name in names)
 Code Generate the following output
LINQ Partitioning operators in ASP.NET

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google