/// <summary> /// 根据条件,返回分页的文档集合 /// </summary> /// <param name="predicate"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public virtual async Task <IPaged <T> > PagedSelectAsync(Expression <Func <T, bool> > predicate, int pageIndex, int pageSize) { if (pageIndex < 1) { pageIndex = 1; } if (pageSize < 1) { pageSize = 20; } var query = Collection.AsQueryable(); if (predicate != null) { query = query.Where(predicate); } var recordTotal = query.Count(); var pageTotal = recordTotal / pageSize; if (recordTotal % pageSize > 0) { pageTotal++; } if (pageIndex > 1) { query = query.Skip((pageIndex - 1) * pageSize); } var records = await query.Take(pageSize).ToListAsync(); var pagedList = new Paged <T>() { PageTotal = pageTotal, RecordTotal = recordTotal, Index = pageIndex, Size = pageSize }; pagedList.AddRange(records); return(pagedList); }
public IPaged <T> Paged(Func <T, bool> predicate, int pageIndex, int pageSize) { var query = _dic.Where(kv => predicate.Invoke(kv.Value)).Select(kv => kv.Value); if (predicate != null) { query = query.Where(predicate); } var enumerable = query as T[] ?? query.ToArray(); var total = enumerable.Count(); var list = enumerable.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); var result = new Paged <T>() { PageTotal = total % pageSize == 0 ? total / pageSize : total / pageSize + 1, RecordTotal = total, Index = pageIndex, Size = pageSize }; result.AddRange(list); return(result); }
/// <summary> /// 根据条件,返回分页的文档集合 /// </summary> /// <param name="filter"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public virtual async Task <IPaged <T> > PagedSelectAsync(FilterDefinition <T> filter, int pageIndex, int pageSize) { if (pageIndex < 1) { pageIndex = 1; } if (pageSize < 1) { pageSize = 20; } var fluent = Collection.Find(filter); var recordTotal = fluent.Count(); var pageTotal = recordTotal / pageSize; if (recordTotal % pageSize > 0) { pageTotal++; } if (pageIndex > 1) { fluent = fluent.Skip((pageIndex - 1) * pageSize); } var records = await fluent.Limit(pageSize).ToListAsync(); var pagedList = new Paged <T>() { PageTotal = (int)pageTotal, RecordTotal = (int)recordTotal, Index = pageIndex, Size = pageSize }; pagedList.AddRange(records); return(pagedList); }