public PagedResult <TEntity> GetModel( Expression <Func <TEntity, bool> > expression, Dictionary <Expression <Func <TEntity, object> >, bool> fields, int pageIndex, int pageSize) { SortDefinition <TEntity> sorts = new ObjectSortDefinition <TEntity>(new { }); foreach (var item in fields) { if (item.Value) { sorts = sorts.Ascending(item.Key); } else { sorts = sorts.Descending(item.Key); } } var skip = (pageIndex - 1) * pageSize; var limit = pageSize; var recordCount = _table.CountAsync <TEntity>(expression).Result; return(new PagedResult <TEntity>( recordCount, (int)(recordCount + pageSize - 1) / pageSize, pageSize, pageIndex, _table.Find(expression) .Sort(sorts) .Skip(skip) .Limit(limit) .ToListAsync().Result)); }
public IEnumerable <TEntity> GetPaged(int pageIndex, int pageSize, Expression <Func <TEntity, object> > sortBy, Expression <Func <TEntity, bool> > where, bool isDesc = false) { SortDefinition <TEntity> sorts = new ObjectSortDefinition <TEntity>(new { }); if (isDesc) { sorts = sorts.Descending(sortBy); } else { sorts = sorts.Ascending(sortBy); } return(_collection.Find(where).Sort(sorts).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToEnumerable()); }
public PagedResult <TEntity> GetModel <U, O>(U template, O orderby, int pageIndex, int pageSize) { #region 条件过滤 BsonDocumentFilterDefinition <TEntity> filterDefinition = GeneratorMongoQuery(template); #endregion #region 排序处理 SortDefinition <TEntity> sorts = new ObjectSortDefinition <TEntity>(new { }); foreach (var item in typeof(O).GetProperties()) { if ((OrderType)item.GetValue(orderby) == OrderType.Asc) { sorts = sorts.Ascending(item.Name); } else { sorts = sorts.Descending(item.Name); } } #endregion #region 分页处理 var skip = (pageIndex - 1) * pageSize; var recordCount = _table.Find(filterDefinition).CountAsync(new CancellationToken()).Result; var limit = pageSize; return(new PagedResult <TEntity>( recordCount, (int)(recordCount + pageSize - 1) / pageSize, pageSize, pageIndex, _table.Find(filterDefinition) .Sort(sorts) .Skip(skip) .Limit(limit) .ToListAsync().Result)); #endregion }
/// <summary> /// 获取集合 /// </summary> /// <param name="pageIndex">获取第几页 从0开始(小于0时强制为0)</param> /// <param name="pageSize">每页数量 为0时所有</param> /// <param name="predicate">条件</param> /// <param name="sorting">排序</param> /// <param name="count">条件数据总数</param> /// <returns>IQueryable[TEntity] 集合 or null </returns> public List <TEntity> Query(int pageIndex, int pageSize, Expression <Func <TEntity, bool> > predicate, List <KeyValuePair <string, string> > sorting, out long count) { List <TEntity> list = new List <TEntity>(); //默认条件 if (predicate == null) { predicate = x => true; } predicate = predicate.And(x => x.IsDelete == false); //总数 count = this.collection.Count(predicate); //初始 var query = this.collection.Find(predicate); //排序 if (sorting == null || (sorting != null && sorting.Count == 0)) { SortDefinition <TEntity> sorts = new ObjectSortDefinition <TEntity>(new { }); PropertyInfo property = typeof(TEntity).GetProperty("Sort"); if (property != null) { sorts = Builders <TEntity> .Sort.Descending("Sort"); } sorts = sorts.Descending(x => x.CreateDt); query = query.Sort(sorts); } #region 排序完整的先注释 //var idx = 0; //sorting.ForEach(x => //{ // idx++; // if (idx == 1) // { // query = sort.IsAscending ? query.SortBy(x=>x.CreateDt) : data.SortByDescending(sort.Field); // } // else // { // var sortByData = data as IOrderedFindFluent<T, T>; // if (sortByData == null) // { // continue; // } // data = sort.IsAscending ? sortByData.ThenBy(sort.Field) : sortByData.ThenByDescending(sort.Field); // } //}); #endregion #region 分页,默认从0开始,pageSize=0时所有 //计算跳过数 int skipCount = 0; if (pageIndex <= 0) { pageIndex = 0; } if (pageSize > 0) { skipCount = pageIndex * pageSize; } query = query.Skip(skipCount); //如果pageSize>0获取指定数量 if (pageSize > 0) { query = query.Limit(pageSize); } #endregion list = query.ToList(); if (list == null) { list = new List <TEntity>(); } return(list); }