Back to: LINQ Tutorial For Beginners and Professionals
Paging Using Skip and Take Method in C#
In this article, I am going to discuss How to implement Paging Using LINQ Skip and Take Method in C# with an Example. Before proceeding to this article, I strongly recommended you read LINQ Skip Method and LINQ Take Method articles where we discussed the Skip and Take method in detail. As part of this article, we are going to discuss the following concepts.
- Why do we need a Paging in Real-time application?
- What is paging?
- Advantages and Disadvantages of using Paging.
- Example of implementing Paging using Skip and Take method in C#
Why do we need Paging in Real-time Applications?
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.
- Network issue as huge data is traveled over the network.
- Memory Management (Due to heavy data showing in the view, it may cause memory issues and maybe the page becomes unresponsive and sometimes the page becomes very slow).
- Performance (We will get bad performance as it takes more time to travel in the network as well as more time to display the data in the page).
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. On the page, we need to display a certain number of records. And next records can be visible with Next and Previous buttons or Page Scrolling or using Page Number or using any other techniques.
Advantages of using Paging:
We will get the following advantages
- Faster Data Transfer. This is because fewer data will be traveled over the network.
- Improve Memory Management. This is because we are not storing all the data on the page.
- Better Performance. As less data is transferred over the network and as we are displaying less number of records in a page, it will definitely give you a better performance.
What is the Drawback of Paging?
In a Client-Server Architecture, the number of requests between the client and server is increased. In such cases, you may get the data at once from the server (from any API or from the Databases) and store it locally in the client and then implement the paging on the Client-Side.
How to Implement Paging using C#?
In C#, we can implement the paging using the LINQ Skip and Take Method. In order to implement Paging, we need to understand two things one is Page Number (PN) and the other one is the Number of Records per Page (NRP). Let’s say Page Number = PN and Number Of Records Per Page = NRP, then you need to use the following formula to implement Paging.
Result = DataSource.Skip((PN – 1) * NRP).Take(NRP)
So, basically from the Data Source, we need to skip (PN – 1) * NRP number of records from the beginning and return NRP from the data source. If this is not clear at the moment, then don’t worry, once we discussed the example, then definitely, you will understand the above formula.
Example to Implement Paging using LINQ Skip and Take Method in C#
Let us see how we can implement Paging in C# using LINQ Skip and Take Methods. 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. Here, you can see, it is a very simple class having 3 properties i.e. ID, Name, and Department. The following class also has one method called GetAllEmployees() which is returning a list of all employees and this list of employees is going to be our data source.
using System.Collections.Generic; namespace LINQPagingDemo { 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 is something that looks as shown in the below image.
In the following C# Console Application, we implement Paging using the LINQ Skip and Take Methods. 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. This problem we will not face when implementing Paging in Web Applications. The following example code is self-explained, so please go through the comment lines for a better understanding.
using System; using System.Linq; namespace LINQPagingDemo { class Program { static void Main(string[] args) { //We want to display 4 Records Per page int RecordsPerPage = 4; //Set the Initial Page Number as 0 int PageNumber = 0; do { Console.WriteLine("\nEnter the Page Number Between 1 and 4"); //Read the Value and if its integer type, then store that value in the PageNumber variable if (int.TryParse(Console.ReadLine(), out PageNumber)) { //Check if PageNumber is > 0 and < 5 if (PageNumber > 0 && PageNumber < 5) { //Logic to Implement Paging var employees = Employee.GetAllEmployees() //Data Source .Skip((PageNumber - 1) * RecordsPerPage) //Skip Logic .Take(RecordsPerPage).ToList(); //Take Logic 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("\nPlease Enter a Valid Page Number"); } } else { Console.WriteLine("\nPlease Enter a Valid Page Number"); } } while (true); } } }
Output:
In the next article, I am going to discuss the LINQ Range Method in C# with Examples. In this article, I try to explain How to Implement Paging Using the LINQ Skip and Take Methods in C# with an Example. I hope you enjoy this article.