February 3, 2011

Paging Data using LINQ

How to use Linq To Sql  refer my below post

To perform paging, below is the code using LINQ


<%@ 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">
    <form id="form1" runat="server">
         <asp:GridView ID="GridView1" runat="server"/>


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;

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

No comments:

Post a Comment