Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Paginate method

I have a problem with a PaginatedList in a Web API project.

In the repository there's a method like:

public virtual PaginatedList<T> Paginate<TKey>(int pageIndex, int pageSize,
    Expression<Func<T, TKey>> keySelector,
    Expression<Func<T, bool>> predicate,
    params Expression<Func<T, object>>[] includeProperties)
{
    IQueryable<T> query = AllIncluding(includeProperties).OrderBy(keySelector);

    query = (predicate == null)
        ? query
        : query.Where(predicate);

    return query.ToPaginatedList(pageIndex, pageSize);
}

But, when I try to use it, like this:

var a = repository.Paginate<Region>(pageNo, pageSize, x => x.ID, null);

I get this error:

Cannot implicitly convert type 'int' to 'Domain.Entities.Dictionaries.Region'

What am I doing wrong?

like image 692
Roman Marusyk Avatar asked Nov 25 '15 20:11

Roman Marusyk


People also ask

What is paginate () in Laravel?

The paginate method provided by Laravel automatically takes care of setting the proper limit and offset based on the current page being viewed by the user. By default, the current page is detected by the value of the ? page query string argument on the HTTP request.

What is the pagination function?

Pagination is used in some form in almost every web application to divide returned data and display it on multiple pages within one web page. Pagination also includes the logic of preparing and displaying the links to the various pages. Pagination can be handled client-side or server-side.

What is paginate mean?

1 : the action of paging : the condition of being paged. 2a : the numbers or marks used to indicate the sequence of pages (as of a book) b : the number and arrangement of pages or an indication of these.


1 Answers

Your method signature has TKey that i suppose is a key for sorting, but in your call you are specifying the whole object Region, and then you specify int in the keySelector, so it can't compile it as it tries to use int type as Region type for TKey.

I suppose your sample should be:

repository.Paginate<int>(pageNo, pageSize, x => x.ID, null);

Generic type T i suppose is specified for the whole class, so it should be fine here to not specify it in the call, as repository instance is already generic specific.

like image 144
Sergey Litvinov Avatar answered Sep 27 '22 23:09

Sergey Litvinov