Paging Using Skip and Take Method

Paging Using Skip and Take Method

In this article, I am going to discuss how to implement Paging Using Skip and Take Method of Linq. Before proceeding to this article, I strongly recommended you to read Skip Method in Linq and Take Method in Linq articles where we discussed the Skip and Take method in detail. As part of this article, we are going to discuss the following concepts.

  1. Why do we need Paging in Real-time application?
  2. What is paging?
  3. Advantages and disadvantages of using Paging.
  4. Example of implementing Paging using Skip and Take method in C#.NET
Why we need Paging in Real-time application?

Suppose we have a data source with lots of records and we need to display those records in a view. We can display all the records in a view at once. If we do so then we will get the following disadvantages.

  1. Network issue (As huge data is traveled)
  2. Memory Management (Due to heavy data showing in the view, it may cause memory issue and maybe the page becomes unresponsive)
  3. Performance (we will get bad performance as it takes more time to travel in the network)

So, in order to solve the above problems, we need to go for Paging.

What is paging?

Paging is nothing but a process in which we will divide a large data source into multiple pages. At one page we need to display a certain number of records. And next records can be visible with next – previous buttons or scroll or using any other techniques.

Advantages of using Paging:

We will get the following advantages

  1. Faster data transfer. This is because fewer data will be traveled in the network.
  2. Improve memory management. This is because we are not showing all the data in a view.
  3. Better performance.

Drawback:

In a client-server architecture, the number of request between the client and server is increased. In such cases, you may get the data at once and store it locally and then implement the paging at the client-side.

How to implement paging?

We can implement the paging using the Linq Skip and Take method. Here we need to understand two things one is PageNumber and the other one is the number of records per page.

Let say Page Number = PN and Number Of Records Per Page = NRP, then you need to use the following formula

Result = DataSource.Skip((PN – 1) * NRP).Take(NRP)

Example:

Here we are going to use the following Employee class. So, create a class file with the name Employee.cs and then copy and paste the following code.

using System.Collections.Generic;
namespace LINQDemo
{
    public class Employee
    {
        Public int ID { get; set; }
        public string Name { get; set; }
        public string Department { get; set; }

        public static List<Employee> GetAllEmployees()
        {
            return new List<Employee>()
            {
                new Employee() {ID = 1, Name = "Pranaya", Department = "IT" },
                new Employee() {ID = 2, Name = "Priyanka", Department = "IT" },
                new Employee() {ID = 3, Name = "Preety", Department = "IT" },
                new Employee() {ID = 4, Name = "Sambit", Department = "IT" },
                new Employee() {ID = 5, Name = "Sudhanshu", Department = "HR" },
                new Employee() {ID = 6, Name = "santosh", Department = "HR" },
                new Employee() {ID = 7, Name = "Happy", Department = "HR" },
                new Employee() {ID = 8, Name = "Raja", Department = "IT" },
                new Employee() {ID = 9, Name = "Tarun", Department = "IT" },
                new Employee() {ID = 10, Name = "Bablu", Department = "IT" },
                new Employee() {ID = 11, Name = "Hina", Department = "HR" },
                new Employee() {ID = 12, Name = "Anurag", Department = "HR" },
                new Employee() {ID = 13, Name = "Dillip", Department = "HR" },
                new Employee() {ID = 14, Name = "Manoj", Department = "HR" },
                new Employee() {ID = 15, Name = "Lima", Department = "IT" },
                new Employee() {ID = 16, Name = "Sona", Department = "IT" },
            };
        }
    }
}
Business Requirement:

First, the program should prompt the user to enter the page number. The page number must be between 1 and 4. If the user does not enter a valid page number, then the program should prompt the user to enter a valid page number. Once the user entered a valid page number, then the program should display the data.

For example, the output of the program something looks as shown below.

Paging Using Skip and Take Method

Note: The condition in the do-while loop puts the program in an infinite loop. In order to end the program, simply close the console window. 

The following console application implements paging using the Skip and Take method of Linq.

using System;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int RecordsPerPage = 4;
            int PageNumber = 0;

            do
            {
                Console.WriteLine("Enter the Page Number between 1 and 4");

                if (int.TryParse(Console.ReadLine(), out PageNumber))
                {
                    if(PageNumber > 0 && PageNumber < 5)
                    {
                        var employees = Employee.GetAllEmployees()
                                    .Skip((PageNumber - 1) * RecordsPerPage)
                                    .Take(RecordsPerPage).ToList();

                        Console.WriteLine();
                        Console.WriteLine("Page Number : " + PageNumber);
                        foreach (var emp in employees)
                        {
                            Console.WriteLine($"ID : {emp.ID}, Name : {emp.Name}, Department : {emp.Department}");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please Enter a Valid Page Number");
                    }
                }
                else
                {
                    Console.WriteLine("Please Enter a Valid Page Number");
                } 
            } while (true);
        }
    }
}

In the next article, I am going to discuss the Linq Range Method with some examples. In this article, I try to explain how to implement Paging Using Skip and Take method. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *