February 3, 2011

Paging Data using LINQ


How to use Linq To Sql  refer my below post
http://www.dotnetissues.com/2011/02/select-query-linq-to-sql-example.html

To perform paging, below is the code using LINQ

Default5.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:GridView ID="GridView1" runat="server"/>
    </div>
    </form>
</body>
</html>

Default5.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var results = from o in db.Orders
                      select o;


        var results1 =results.Skip(2).Take(5);
        GridView1.DataSource = results1;
        GridView1.DataBind();
    }
}


The above query will skip first 2 records , and keep the next 5 records fetched 
from the query  

No comments:

Post a Comment