Data Pagination with Extension Methods in C#

Introduction

Pagination is a common technique used to manage and display data in manageable chunks. Extensions in C# provide a powerful way to implement pagination in a clean and reusable manner.

Extension Methods?

  • Extension methods allow you to add new methods to existing types without modifying their source code.
  • Extension methods are particularly useful when you need to extend the functionality of classes you do not own or cannot change.
  • Extension methods are defined as static methods in a static class, but they are called as if they were instance methods on the extended type.

Steps

  1. Define the Static Class and Method: Create a static class to hold the extension method.
  2. Implement the Extension Method: Write the method to take page size and page no as parameters and return the page subset of data.

Lets create extension for IQueryable<T> & IEnumerable<T>

  1. Creating a Paging Extension Method using Default Values.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Extensions
    {
        public static class PagingExtensions
        {
            public static IQueryable<T> Paging<T>(this IQueryable<T> source, int pageNo, int pageSize)
            {
                if (pageNo < 1)
                {
                    pageNo = 1;
                }
    
                if (pageSize < 1)
                {
                    pageSize = 25;
                }
    
                return source.Skip((pageNo - 1) * pageSize).Take(pageSize);
            }
    
            public static IEnumerable<T> Paging<T>(this IEnumerable<T> source, int pageNo, int pageSize)
            {
                if (pageNo < 1)
                {
                    pageNo = 1;
                }
    
                if (pageSize < 1)
                {
                    pageSize = 25;
                }
    
                return source.Skip((pageNo - 1) * pageSize).Take(pageSize);
            }
        }
    }
  2. Creating a Paging Extention Method without Default Values.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Extensions
    {
        public static class PagingExtensions
        {
            public static IQueryable<T> Paging<T>(this IQueryable<T> source, int pageNo, int pageSize)
            {
                if (pageNo < 1)
                {
                    throw new ArgumentException("Page no must be greater than 0.");
    
                }
                if (pageSize < 1)
                {
                    throw new ArgumentException("Page size must be greater than 0.");
                }
    
                return source.Skip((pageNo - 1) * pageSize).Take(pageSize);
            }
    
            public static IEnumerable<T> Paging<T>(this IEnumerable<T> source, int pageNo, int pageSize)
            {
                if (pageNo < 1)
                {
                    throw new ArgumentException("Page no must be greater than 0.");
                }
    
                if (pageSize < 1)
                {
                    throw new ArgumentException("Page size must be greater than 0.");
                }
    
                return source.Skip((pageNo - 1) * pageSize).Take(pageSize);
            }
        }
    }
    

Conclusion

Extension methods offer a clean and efficient way to implement pagination in C#. By using these methods, you can simplify your data handling code and improve readability. This approach not only saves time but also ensures that your pagination logic is reusable and easy to maintain.

Try these extensions and drop me your feedback.


Recommended Free Ebook
Similar Articles