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


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)







    class Employee


        public int ID { get; set; }

        public string FName { get; set; }

        public DateTime DOB { get; set; }





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


        Next r


    End Sub


    Private Class Employee

        Private privateID As Integer

        Public Property ID() As Integer


                Return privateID

            End Get

            Set(ByVal value As Integer)

                privateID = value

            End Set

        End Property

        Private privateFName As String

        Public Property FName() As String


                Return privateFName

            End Get

            Set(ByVal value As String)

                privateFName = value

            End Set

        End Property

        Private privateDOB As DateTime

        Public Property DOB() As DateTime


                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.

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: