如何使用分页方法 [英] How to use Paginate method
问题描述
我在Web API项目PaginatedList一个问题。
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:
无法键入'诠释'隐式转换为
Domain.Entities.Dictionaries.Region
Cannot implicitly convert type 'int' to 'Domain.Entities.Dictionaries.Region'
我在做什么错了?
推荐答案
您方法签名 TKEY的
,我想是排序的关键,但在你的电话你指定整个对象地区
,然后指定 INT
在的KeySelectors
,这样它会尝试使用它不能编译 INT
键入为地区
键入 TKEY的
。
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
.
我想你的样品应该是:
repository.Paginate<int>(pageNo, pageSize, x => x.ID, null);
泛型类型 T
我想是全班指定的,所以它应该是罚款这里在调用没有指定它,因为库实例已经通用的具体。
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.
这篇关于如何使用分页方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!