我必須查詢一個表中的所有行。在一個鏡頭中,查詢太長,我得到一個超時。所以我想使用分頁在幾個批次中運行這個查詢。但要做size
和take
orderby是必需的。這就是問題,如何通過通用(可能在主鍵上)進行訂單?或者有沒有沒有秩序的方式?實體泛型分頁
public async Task<IEnumerable<T>> GetListBatchLoadingAsync<T>(Expression<Func<T, bool>> iWhere = null, long iSize) where T : class, IEntity
{
IQueryable<T> theQueryable = RepositoryDBContext.Set<T>();
//Where clause
if (iWhere != null)
theQueryable = theQueryable.Where(iWhere);
//batch loading
Int64 entitiesCount = await theQueryable.CountAsync();
int totalPage = (int)Math.Ceiling(decimal.Divide(entitiesCount, iSize));
//query
var result = new List<T>();
for (int a = 0; a < totalPage; a++)
result.AddRange(await theQueryable.OrderBy(x=>x. ????).Skip(TAKECOUNT * (a)).Take(TAKECOUNT).ToListAsync());
return result;
}
謝謝你的回答。但我最大的問題是通過keyselector以通用方式設置訂單。 – Julian50