2015-11-25 33 views
6

我在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'

我究竟做錯了什麼?

+0

您的樣品是否正確?你的'Paginate'方法有4個參數(忽略參數),但是在樣本調用中只打磨3個 –

+0

是的,對了。這只是一個錯誤。我已經更新了問題 – Marusyk

回答

6

你的方法簽名TKey,我想是排序的關鍵,但在你的電話是否指定了整個對象Region,然後你在keySelector指定int,所以因爲它試圖不能編譯使用int類型爲Region類型爲TKey

我想你的樣品應該是:

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

泛型類型T我想全班指定,所以它應該是罰款這裏在調用沒有指定它,因爲庫實例已經通用具體。

+0

非常感謝。其作品。再次感謝。 – Marusyk

+0

歡迎:) –