jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Sorting

nurlybekovnt opened this issue · comments

Hi, Jason!
Now I have common pagination tool like this.

public class PageableVm<T>
{
    public IEnumerable<T> Items { get; }
    public int PageIndex { get; }
    public int PageSize { get; }
    public int TotalCount { get; }
    public PageableVm(IEnumerable<T> items, int count, int pageIndex, int pageSize)
    {
        Items = items;
        TotalCount = count;
        PageIndex = pageIndex;
        PageSize = pageSize;
    }
    public int TotalPages => (int) Math.Ceiling(TotalCount / (double) PageSize);
    public bool HasPreviousPage => PageIndex > 1;
    public bool HasNextPage => PageIndex < TotalPages;
    public static async Task<PageableVm<T>> CreateAsync(IQueryable<T> source, int? pageIndex, int? pageSize)
    {
        var index = pageIndex ?? 1;
        var size = pageSize ?? 30;
        var items = await source.Skip((index - 1) * size).Take(size).ToListAsync();
        return new PageableVm<T>(items, await source.CountAsync(), index, size);
    }
}

And it is very cool, I use it like this:

return await PageableVm<ContactDto>
    .CreateAsync(contacts.ProjectTo<ContactDto>(_mapper.ConfigurationProvider),
        query.PageIndex,
        query.PageSize);

How to implement common sorting tool?

Thank you for your interest in this project. This repository has been archived and is no longer actively maintained or supported. We appreciate your understanding. Feel free to explore the codebase and adapt it to your own needs if it serves as a useful reference. If you have any further questions or concerns, please refer to the README for more information.