Roman Marusyk
Roman Marusyk

Reputation: 24609

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?

Upvotes: 6

Views: 262

Answers (1)

Sergey Litvinov
Sergey Litvinov

Reputation: 7478

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.

Upvotes: 5

Related Questions