How To Implement Paging Using Skip And Take Operators In LINQ

Introduction

In this article, I will demonstrate how we can use skip and take operator of Language-Integrated Query (LINQ) for the implementation of paging in C# programming. Skip and take operators are part of partitioning data in Language-Integrated Query (LINQ).

Partitioning Data

Partitioning in LINQ refers to the operation of dividing an input sequence into two sections, without rearranging the elements, and then returning one of the sections.

Skip

Skips elements up to a specified position in a sequence.

Take

Takes elements up to a specified position in a sequence.

Step 1

Create console application in Visual Studio 2015.

Step 2

Create class, name it employee, and add some records in the list of the employee.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Partitioning_Data  
  8. {  
  9.     class Employee  
  10.     {  
  11.         public int EMPID { get; set; }  
  12.         public string Name { get; set; }  
  13.         public string Position { get; set; }  
  14.         public int Salary { get; set;}  
  15.   
  16.         public static List<Employee> GetAllEmployees()  
  17.         {  
  18.             List<Employee> listEmployee = new List<Employee>  
  19.             {  
  20.                 new Employee {  
  21.                     EMPID=100,Name="Fayaz",Position="UI Developer",Salary=50000  
  22.                 },  
  23.                 new Employee {  
  24.                     EMPID=101,Name="Firoz",Position="JavaScript Developer",Salary=40000  
  25.                 },  
  26.                 new Employee {  
  27.                     EMPID=102,Name="Rahul",Position="Anroid Developer",Salary=45000  
  28.                 },  
  29.                 new Employee {  
  30.                     EMPID=103,Name="Priya",Position="Windows Developer",Salary=60000  
  31.                 },  
  32.                 new Employee {  
  33.                     EMPID=104,Name="Monica",Position="ASP.Net Developer",Salary=80000  
  34.                 },  
  35.                 new Employee {  
  36.                     EMPID=105,Name="Umesh",Position="Salas Excutive",Salary=30000  
  37.                 },  
  38.                 new Employee {  
  39.                     EMPID=106,Name="Danish",Position="Digital Marketing",Salary=25000  
  40.                 },  
  41.                 new Employee {  
  42.                     EMPID=107,Name="Mohit",Position="Marketing",Salary=18000  
  43.                 },  
  44.                 new Employee {  
  45.                     EMPID=108,Name="Pankaj",Position="Salas Excutive",Salary=35000  
  46.                 },  
  47.                 new Employee {  
  48.                     EMPID=109,Name="Sangeeta",Position="UI Developer",Salary=28000  
  49.                 },  
  50.                 new Employee {  
  51.                     EMPID=110,Name="Uma",Position="Salas Excutive",Salary=32000  
  52.                 },  
  53.                 new Employee {  
  54.                     EMPID=111,Name="Mukesh",Position="Testing",Salary=48000  
  55.                 },  
  56.                 new Employee {  
  57.                     EMPID=112,Name="Parkash",Position="HR",Salary=42000  
  58.                 },  
  59.                 new Employee {  
  60.                     EMPID=113,Name="Habib",Position="Angular Developer",Salary=58000font  
  61.                 },  
  62.                 new Employee {  
  63.                     EMPID=114,Name="Rajesh",Position="Desginer",Salary=25000  
  64.                 },  
  65.             };  
  66.             return listEmployee;  
  67.         }  
  68.     }  
  69. }  

Step 3

Call employee class in our console application by the default class. Name program using IEnumerable interface and invoke the method GetAllEmployee. Ask the user in the prompt to enter a page number to display the record. Do not allow the user to enter the wrong page number or any word or letter. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Partitioning_Data  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             do {  
  14.                 IEnumerable<Employee> Employees = Employee.GetAllEmployees();  
  15.                 Console.Write("Please Enter Page number 1,2 or 3 \n");  
  16.                 int pageNumber = 0;  
  17.   
  18.                 if (int.TryParse(Console.ReadLine(), out pageNumber))  
  19.                 {  
  20.                     if (pageNumber >= 1 && pageNumber <= 3)  
  21.                     {  
  22.                         int pageSize = 5;  
  23.                         IEnumerable<Employee> result = Employees.Skip((pageNumber - 1) * pageSize).Take(pageSize);  
  24.                         Console.WriteLine();  
  25.                         Console.WriteLine("Displaying page" + pageSize);  
  26.                         foreach (Employee emp in result)  
  27.                         {  
  28.                             Console.WriteLine(emp.EMPID + "\t" + emp.Name + "\t" + emp.Position + "\t" + emp.Salary);  
  29.                         }  
  30.                         Console.WriteLine();  
  31.                     }  
  32.                     else  
  33.                     {  
  34.                         Console.WriteLine("Page number must be integer between 1 and 3");  
  35.                     }  
  36.                 }  
  37.                 else  
  38.                 {  
  39.                     Console.WriteLine("Page number must be integer between 1 and 3");  
  40.                 }  
  41.             } while (1==1);  
  42.         }  
  43.     }  
  44. }  

Step 4

Run the application Ctrl+F5

Output

 

Output

 

Conclusion

We have learned skip and take operator in Language-Integrated Query (LINQ) form portioning data. I hope it will be a helpful article for beginners.


Similar Articles