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

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

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.