我在 Web API 项目中遇到 PaginatedList 问题。
在存储库中有一个方法如下:
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);
}
但是,当我尝试使用它时,就像这样:
var a = repository.Paginate<Region>(pageNo, pageSize, x => x.ID, null);
我收到这个错误:
Cannot implicitly convert type 'int' to 'Domain.Entities.Dictionaries.Region'
我做错了什么?
最佳答案
你的方法签名有 TKey
我想它是排序的关键,但在你的调用中你指定了整个对象 Region
,然后你指定了
,因此它无法编译它,因为它尝试使用 keySelector
中的 intint
类型作为 Region
类型 T键
。
我想你的样本应该是:
repository.Paginate<int>(pageNo, pageSize, x => x.ID, null);
通用类型 T
我想是为整个类指定的,所以在这里不在调用中指定它应该没问题,因为存储库实例已经是通用特定的。
关于c# - 如何使用分页方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925750/