Search

Monday, April 14, 2014
0 comments

LINQ Partitioning operators in ASP.NET

6:05 AMMonday, April 14, 2014

Introduction

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
<div>
    
        <asp:ListBox ID="ListBox1" runat="server" Height="134px" Width="197px">
        </asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text=" Partitioning Operator Linq" />
    
    </div>

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)
            {
                ListBox1.Items.Add(name);
            }
            
        }
             
    }
 Code Generate the following output
LINQ Partitioning operators in ASP.NET

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top