Click here to Skip to main content
Click here to Skip to main content

Effective Paging Using LINQ (Sample Code)

By , 13 Sep 2012
 

Introduction

Binding gridview with paging using LINQ is a nice way to bind large amount of data with efficient speed. Two LINQ operators Skip and Take make this task very easy.

Background 

HOW DO WE ACHIEVE EFFECTIVE PAGING FROM LINQ ON SERVER SIDE ? 

If you are familiar with LINQ and its techniques, then you must read about Deferred Execution of LINQ. Deferred Execution in LINQ depicts that LINQ executes query when it is about to use, means when we are using var of LINQ, not when we write it. You can learn more about deferred execution here

Using the Code

Skip and Take operators allow you to only pull back the records you need. 

If you are not interested in reading this article, you can simply download the code from the link at the top of this article. There, you will get the sample code of this article.

We can achieve effective paging by the following methods:

  1. Stored procedure
  2. From LINQ 

In the simple example below, I am using a LinqDataSource and handling its onselecting method to create our LINQ query and achieving effective paging. Set AutoPage to false because I am writing code to handle paging ourselves. Also the PageSize property of the GridView control is being populated from a integer constant in the code-behind class. 

On Aspx Page

<asp:gridview allowpaging="True" autogeneratecolumns="False" 
datasourceid="LinqDataSource1" id="GridView1" pagesize="<%# PAGE_SIZE %>" 
runat="server" width="276px">
        <columns>
            <asp:boundfield datafield="ProductID" headertext="PID" readonly="True" 
		sortexpression="ProductID">
            <asp:boundfield datafield="ProductName" headertext="ProductName" 
		readonly="True" sortexpression="ProductName">
            <asp:boundfield datafield="UnitPrice" headertext="UnitPrice" 
		readonly="True" sortexpression="UnitPrice">
            
        </asp:boundfield></asp:boundfield></asp:boundfield></columns>
        </asp:gridview>
        

        <asp:linqdatasource autopage="False" contexttypename="DataClassesDataContext" 
		entitytypename="" id="LinqDataSource1" onselecting="LinqDataSource1_Selecting" 
	runat="server" select="new (ProductID, ProductName, UnitPrice)" tablename="Products">
        </asp:linqdatasource>   

On Code Behind   

DataClassesDataContext db = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    }
}
public const int PAGE_SIZE = 10;

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    // LINQ query


    var query = from m in db.Products
                select m;

    // Set the total count
    // so GridView knows how many pages to create
    e.Arguments.TotalRowCount = query.Count();

    // Get only the rows we need for the page requested
    query = query.Skip(GridView1.PageIndex * PAGE_SIZE).Take(PAGE_SIZE);

    e.Result = query;
}

Download the sample code for LINQ effective paging and paging from the stored procedure.

If this tip is helpful, then don't forget to say thanks. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sarvesh Kushwaha
Software Developer
India India
Member
I do believe life is to help others ... So here i am .. in my spare time i learn new things of programming and try to help people with my knowledge .
I'm an energetic, self-motivated and hard-working Developer and Information Technology Professional with experience in projects, website design and development.
 
Visit My Technical Blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionnice examplememberMember 98749681 Mar '13 - 0:48 
AnswerRe: nice examplememberSarvesh Kushwaha1 Mar '13 - 14:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 14 Sep 2012
Article Copyright 2012 by Sarvesh Kushwaha
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid