Menus

Tuesday, December 4, 2012

Implementing Paging in a Generic List using LINQ

It's quite common of users to bind their Generic List to an ASP.NET control. However if the list is huge, you may need to implement Paging functionality. Here's a simple way to do implement Paging functionality using LINQ

C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



public partial class LINQ : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        List<Employee> empList = new List<Employee>();

        empList.Add(new Employee() { ID = 1, FName = "John",  DOB = DateTime.Parse("12/11/1971")});

        empList.Add(new Employee() { ID = 2, FName = "Mary",  DOB = DateTime.Parse("01/17/1961")});

        empList.Add(new Employee() { ID = 3, FName = "Amber", DOB = DateTime.Parse("12/23/1971")});

        empList.Add(new Employee() { ID = 4, FName = "Kathy", DOB = DateTime.Parse("11/15/1976")});

        empList.Add(new Employee() { ID = 5, FName = "Lena",  DOB = DateTime.Parse("05/11/1978")});

 

        var records = from emp in empList

                      select emp;

        var pgNo = 1;

        var pgRec = 2;

        records = records.Skip((pgNo - 1) * pgRec).Take(pgRec);



        foreach (var r in records)

        {

            Console.WriteLine(r.FName);

        }



    }



    class Employee

    {

        public int ID { get; set; }

        public string FName { get; set; }

        public DateTime DOB { get; set; }

    }



}



VB.NET

Imports System

Imports System.Collections.Generic

Imports System.Linq

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls



Partial Public Class LINQ

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        Dim empList As New List(Of Employee)()

        empList.Add(New Employee() With {.ID = 1, .FName = "John", .DOB = DateTime.Parse("12/11/1971")})

        empList.Add(New Employee() With {.ID = 2, .FName = "Mary", .DOB = DateTime.Parse("01/17/1961")})

        empList.Add(New Employee() With {.ID = 3, .FName = "Amber", .DOB = DateTime.Parse("12/23/1971")})

        empList.Add(New Employee() With {.ID = 4, .FName = "Kathy", .DOB = DateTime.Parse("11/15/1976")})

        empList.Add(New Employee() With {.ID = 5, .FName = "Lena", .DOB = DateTime.Parse("05/11/1978")})



        Dim records = _

         From emp In empList _

         Select emp

        Dim pgNo = 1

        Dim pgRec = 2

        records = records.Skip((pgNo - 1) * pgRec).Take(pgRec)



        For Each r In records

            Console.WriteLine(r.FName)

        Next r



    End Sub



    Private Class Employee

        Private privateID As Integer

        Public Property ID() As Integer

            Get

                Return privateID

            End Get

            Set(ByVal value As Integer)

                privateID = value

            End Set

        End Property

        Private privateFName As String

        Public Property FName() As String

            Get

                Return privateFName

            End Get

            Set(ByVal value As String)

                privateFName = value

            End Set

        End Property

        Private privateDOB As DateTime

        Public Property DOB() As DateTime

            Get

                Return privateDOB

            End Get

            Set(ByVal value As DateTime)

                privateDOB = value

            End Set

        End Property

    End Class



End Class

Observe how we have used the Skip and Take function on the List.

No comments:

Post a Comment