Pagination.EntityFrameworkCore.Extensions 2.5.6

dotnet add package Pagination.EntityFrameworkCore.Extensions --version 2.5.6                
NuGet\Install-Package Pagination.EntityFrameworkCore.Extensions -Version 2.5.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pagination.EntityFrameworkCore.Extensions" Version="2.5.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pagination.EntityFrameworkCore.Extensions --version 2.5.6                
#r "nuget: Pagination.EntityFrameworkCore.Extensions, 2.5.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Pagination.EntityFrameworkCore.Extensions as a Cake Addin
#addin nuget:?package=Pagination.EntityFrameworkCore.Extensions&version=2.5.6

// Install Pagination.EntityFrameworkCore.Extensions as a Cake Tool
#tool nuget:?package=Pagination.EntityFrameworkCore.Extensions&version=2.5.6                

Pagination.EntityFrameworkCore.Extensions

This is a library for List Pagination on Dotnet. Works well with Entity Framework as an extension.

Get Started

Install-Package Pagination.EntityFrameworkCore.Extensions

Sample output on Web API:

{
  "totalItems": 5,
  "currentPage": 2,
  "nextPage": 3,
  "previousPage": 1,
  "totalPages": 3,
  "results": [
    {
      "name": "Mali",
      "id": "0ae45b80-9269-4fa7-86b3-e6b294cba322",
      "dateAdded": "2021-09-26T15:31:14.936849+02:00",
      "lastModifiedDate": "2021-09-26T15:31:14.936849+02:00"
    },
    {
      "name": "South Africa",
      "id": "0ae45b80-9269-4fa7-86b3-e6b494cba13e",
      "dateAdded": "2021-03-15T21:02:03.8421801+00:00",
      "lastModifiedDate": "2021-03-15T21:02:03.8422383+00:00"
    }
  ]
}

How to use:

Support both asynchronous vs synchronous methods, only "Async" at the end of method name will differentiate.

// Use only Pagination Model
public async Task<Pagination<Country>> GetCountriesAsync(int page, int limit)
{
	var list  = await _dbContext.Countries.Skip((page - 1) * limit).Take(limit).ToListAsync();
	var totalItems  = await _dbContext.Countries.CountAsync();
	return new Pagination<Country>(list, totalItems, page, limit);

	//OR, there is optional boolean param applyPageAndLimitToResults if you want to apply page and limit to results
	/*
	var list  = await _dbContext.Countries.ToListAsync();
	var totalItems  = await _dbContext.Countries.CountAsync();

	return new Pagination<Country>(list, totalItems, page, limit, applyPageAndLimitToResults: true);
	
	*/
}

// OR use as Entity Framework extension
private async Task<Pagination<Country>> GetAllCountriesAsync(int page, int limit)
{
	using (var context = new CollegeDbContext())
	{
		return await _dbContext.Countries.AsPaginationAsync<Country>(page, limit, sortColumn: "Name", orderByDescending: true);
		// OR return await _dbContext.AsPaginationAsync<Country>(page, limit, sortColumn: "Name", orderByDescending: true);
	}
}

//OR add filter before pagination
private async Task<Pagination<Country>> GetAllCountriesAsync(int page, int limit)
{
	using (var context = new CollegeDbContext())
	{
		return await _dbContext.Countries.Where(x => x.DateAdded > DateTimeOffset.UtcNow.AddDays(-30)).AsPaginationAsync<Country>(page, limit, sortColumn: "Name", orderByDescending: true);
	}
}

//OR with supported filter
private async Task<Pagination<Country>> GetAllCountriesAsync(int page, int limit, string searchText)
{
	using (var context = new CollegeDbContext())
	{
		return await _dbContext.Countries.AsPaginationAsync<Country>(page, limit, x => x.Name.Contains(searchText), sortColumn: "Name", orderByDescending: true);
		// OR return await _dbContext.AsPaginationAsync<Country>(page, limit, x => x.Name.Contains(searchText), sortColumn: "Name", orderByDescending: true);
	}
}
    
    

Auto Mapping or Converting Models:

//Create a method that will map/convert source model to destination model.
//This method can also be Async.
private CountryViewModel ConverCountryToCountryViewModel(Country country)
{
	return new CountryViewModel
	{
		name = country.Name,
		Id = country.Id
	};
}

// Use only Pagination Model
public async Task<Pagination<CountryViewModel>> GetCountriesAsync(int page, int limit)
{
	var list  = await _dbContext.Countries.Skip((page - 1) * limit).Take(limit).ToListAsync();
	var totalItems  = await _dbContext.Countries.CountAsync();
	//Pass 'ConverCountryToCountryViewModel' method that will map/convert source model to destination model 
	return new Pagination<CountryViewModel>(list, totalItems, ConverCountryToCountryViewModel, page, limit);

	//OR, there is optional boolean param applyPageAndLimitToResults if you want to apply page and limit to results
	/*
	var list  = await _dbContext.Countries.ToListAsync();
	var totalItems  = await _dbContext.Countries.CountAsync();
	return Pagination<Country>.GetPagination<Country, CountryViewModel>(results, totalItems, x => ConverCountryToCountryViewModel(x), page, limit, applyPageAndLimitToResults: true);
	*/
}

// OR use as Entity Framework extension
private async Task<Pagination<CountryViewModel>> GetAllCountriesAsync(int page, int limit)
{
	using (var context = new CollegeDbContext())
	{
		//Pass 'ConverCountryToCountryViewModel' method that will map/convert source model to destination model 
		return await _dbContext.Countries.AsPaginationAsync<Country, CountryViewModel>(page, limit, x => ConverCountryToCountryViewModel(x), sortColumn: "Name", orderByDescending: true);
		//OR return await _dbContext.AsPaginationAsync<Country, CountryViewModel>(page, limit, x => ConverCountryToCountryViewModel(x), sortColumn: "Name", orderByDescending: true);
	}
}

//OR add filter before pagination
private async Task<Pagination<CountryViewModel>> GetAllCountriesAsync(int page, int limit)
{
	using (var context = new CollegeDbContext())
	{
		//Pass 'ConverCountryToCountryViewModel' method that will map/convert source model to destination model 
		return await _dbContext.Countries.Where(x => x.DateAdded > DateTimeOffset.UtcNow.AddDays(-30)).AsPaginationAsync<Country, CountryViewModel>(page, limit, x => ConverCountryToCountryViewModel(x), sortColumn: "Name", orderByDescending: true);
		
	}
}

//OR with supported filter
private async Task<Pagination<CountryViewModel>> GetAllCountriesAsync(int page, int limit, string searchText)
{
	using (var context = new CollegeDbContext())
	{
		//Pass 'ConverCountryToCountryViewModel' method that will map/convert source model to destination model 
		return await _dbContext.Countries.AsPaginationAsync<Country, CountryViewModel>(page, limit, x => x.Name.Contains(searchText), x => ConverCountryToCountryViewModel(x), sortColumn: "Name", orderByDescending: true);
		//OR return await _dbContext.AsPaginationAsync<Country, CountryViewModel>(page, limit, x => x.Name.Contains(searchText), x => ConverCountryToCountryViewModel(x), sortColumn: "Name", orderByDescending: true);
	}
}

// With extra parameter/s
private async Task<CountryViewModel> ConverCountryToCountryViewModelAsync(Country country, Guid userId)
{
//Some async processing like sending audit log somewhere
/*
	await _someLoggingService.LogAsync(userId, country);
*/
	return new CountryViewModel
	{
		name = country.Name,
		Id = country.Id
	};
}
//Then auto map like this:
private async Task<Pagination<CountryViewModel>> GetAllCountriesAsync(int page, int limit, string searchText)
{
	using (var context = new CollegeDbContext())
	{
		//Pass 'ConverCountryToCountryViewModelAsync' method that will map/convert source model to destination model 
		return await _dbContext.Countries.AsPaginationAsync<Country, CountryViewModel>(page, limit, x => x.Name.Contains(searchText), x => ConverCountryToCountryViewModelAsync(x, Guid.Empty), sortColumn: "Name", orderByDescending: true);
		//OR return await _dbContext.AsPaginationAsync<Country, CountryViewModel>(page, limit, x => x.Name.Contains(searchText),  x => ConverCountryToCountryViewModelAsync(x, Guid.Empty), sortColumn: "Name", orderByDescending: true);
	}
}
		
		

NuGet

https://www.nuget.org/packages/Pagination.EntityFrameworkCore.Extensions/

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Pagination.EntityFrameworkCore.Extensions:

Package Downloads
CCNextGen_Template

The bootstrap-based template used by Chemung County for the internal apps turned into a Razor Class Library for easier integration into future proejcts

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.6 5,882 3/28/2024
2.5.1 2,204 10/25/2023
2.4.3 1,747 5/23/2023
2.3.3 8,624 3/18/2023
2.3.2 474 11/13/2022
2.3.0 789 6/4/2022
2.2.1 626 1/24/2022
2.1.7 587 11/21/2021
2.1.6 382 10/12/2021
2.1.5 370 10/12/2021
2.1.4 382 10/11/2021
2.1.3 561 10/11/2021

This is a library for List Pagination on Dotnet, auto map or convert source to destination model.
     Works well with Entity Framework as an extension and supports asynchronous calls.

     More example of project URL https://github.com/SitholeWB/Pagination.EntityFrameworkCore.Extensions