How create Pagination in C#

How to create Pagination Logic in c#.

Code sample..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestInterviewSolution
{
    class Program
    {
        DefaultConnection constring = new DefaultConnection();

        public List<Emp> Getemp()
        {
            List<Emp> d = (from s in constring.emps.ToList() select s).ToList();
            return d;
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            IEnumerable<Emp> emplist = p.Getemp();
            int totalitem= emplist.Count();
            int page= 1;
            int pagesize = 5;
         
            Console.WriteLine("Enter the pagination 1  5  10  15 20" );

            var pagenumber = int.Parse(Console.ReadLine());
            if (pagenumber > 0)
            {
             
                foreach (var i in emplist.Skip(page * pagenumber - 1).Take(pagesize))
                {
                    Console.WriteLine(i.Id + "   " + i.Name + "  " + i.Salary);
                }

            }

         
            Console.ReadLine();
        }
    }
}

Output:- enter 10  and row size(pagesize) is 5








Share this

Previous
Next Post »