Pagination with generic method

3

I'm trying to create a generic paging method, I tried to do as the code below, however when using Skip I need to have a orderby and I'm not sure how to do it.

public IQueryable<TEntity> GetPaginacao(int PageStart, int PageSize)
        {
            return Db.Set<TEntity>()
                .Take(PageSize)
                .Skip(PageStart * PageSize)
                .AsNoTracking();

        }

Error:

  

The method 'OrderBy' should be called before the 'Skip' method

    
asked by anonymous 14.01.2016 / 15:29

1 answer

6

You do not need to reinvent the wheel. Only install this package .

Usage:

var pagina = 1;
var registros = 50;
var selecao = db.Entidade.ToPagedList(pagina, registros);

Or:

var selecao = await db.Entidade.ToPagedListAsync(pagina, registros);

I think in your case you will have to sort by something:

var selecao = db.Entidade.OrderBy(e => e.Id).ToPagedList(pagina, registros);
    
14.01.2016 / 15:35