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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Partitioning_Data
- {
- class Employee
- {
- public int EMPID { get; set; }
- public string Name { get; set; }
- public string Position { get; set; }
- public int Salary { get; set;}
-
- public static List<Employee> GetAllEmployees()
- {
- List<Employee> listEmployee = new List<Employee>
- {
- new Employee {
- EMPID=100,Name="Fayaz",Position="UI Developer",Salary=50000
- },
- new Employee {
- EMPID=101,Name="Firoz",Position="JavaScript Developer",Salary=40000
- },
- new Employee {
- EMPID=102,Name="Rahul",Position="Anroid Developer",Salary=45000
- },
- new Employee {
- EMPID=103,Name="Priya",Position="Windows Developer",Salary=60000
- },
- new Employee {
- EMPID=104,Name="Monica",Position="ASP.Net Developer",Salary=80000
- },
- new Employee {
- EMPID=105,Name="Umesh",Position="Salas Excutive",Salary=30000
- },
- new Employee {
- EMPID=106,Name="Danish",Position="Digital Marketing",Salary=25000
- },
- new Employee {
- EMPID=107,Name="Mohit",Position="Marketing",Salary=18000
- },
- new Employee {
- EMPID=108,Name="Pankaj",Position="Salas Excutive",Salary=35000
- },
- new Employee {
- EMPID=109,Name="Sangeeta",Position="UI Developer",Salary=28000
- },
- new Employee {
- EMPID=110,Name="Uma",Position="Salas Excutive",Salary=32000
- },
- new Employee {
- EMPID=111,Name="Mukesh",Position="Testing",Salary=48000
- },
- new Employee {
- EMPID=112,Name="Parkash",Position="HR",Salary=42000
- },
- new Employee {
- EMPID=113,Name="Habib",Position="Angular Developer",Salary=58000font
- },
- new Employee {
- EMPID=114,Name="Rajesh",Position="Desginer",Salary=25000
- },
- };
- return listEmployee;
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Partitioning_Data
- {
- class Program
- {
- static void Main(string[] args)
- {
- do {
- IEnumerable<Employee> Employees = Employee.GetAllEmployees();
- Console.Write("Please Enter Page number 1,2 or 3 \n");
- int pageNumber = 0;
-
- if (int.TryParse(Console.ReadLine(), out pageNumber))
- {
- if (pageNumber >= 1 && pageNumber <= 3)
- {
- int pageSize = 5;
- IEnumerable<Employee> result = Employees.Skip((pageNumber - 1) * pageSize).Take(pageSize);
- Console.WriteLine();
- Console.WriteLine("Displaying page" + pageSize);
- foreach (Employee emp in result)
- {
- Console.WriteLine(emp.EMPID + "\t" + emp.Name + "\t" + emp.Position + "\t" + emp.Salary);
- }
- Console.WriteLine();
- }
- else
- {
- Console.WriteLine("Page number must be integer between 1 and 3");
- }
- }
- else
- {
- Console.WriteLine("Page number must be integer between 1 and 3");
- }
- } while (1==1);
- }
- }
- }
Step 4
Run the application Ctrl+F5
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.