public Results <TEntity> FindAll(int pageIndex, int pageSize, object sortBy = null) { BsonDocument doc = new BsonDocument(); IFindFluent <TEntity, TEntity> res = FindFluent(doc, pageIndex, pageSize, sortBy); return(new Results <TEntity>(res.ToList(), res.Count(), pageIndex, pageSize)); }
/// <summary> /// 分页获取子类对象列表 /// </summary> /// <param name="condition">条件表达式</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页容量</param> /// <param name="rowCount">总记录条数</param> /// <param name="pageCount">总页数</param> /// <returns>子类对象列表</returns> protected IFindFluent <TSub, TSub> FindByPage <TSub>(Expression <Func <TSub, bool> > condition, int pageIndex, int pageSize, out int rowCount, out int pageCount) where TSub : T { IFindFluent <TSub, TSub> list = this.FindAndSort(condition); rowCount = unchecked ((int)list.Count()); pageCount = (int)Math.Ceiling(rowCount * 1.0 / pageSize); return(list.Skip((pageIndex - 1) * pageSize).Limit(pageSize)); }
public static PagedList <T> ToPageResult <T>(this IFindFluent <T, T> query, int pageIndex, int pageSize) { var pageResult = new PagedList <T>(); var totalCount = query.Count(); var totalPages = (int)totalCount / pageSize; pageResult.TotalPages = totalCount % pageSize == 0 ? totalPages : totalPages + 1; pageResult.Rows = query.Skip(pageIndex * pageSize).Limit(pageSize).ToList(); pageResult.PageIndex = pageIndex; pageResult.TotalCount = (int)totalCount; return(pageResult); }
/// <summary> /// 匹配条件获取分页数据集合(异步) /// </summary> /// <param name="Match"></param> /// <param name="Info"></param> /// <returns></returns> public async Task <IList <T> > FindWithPagerAsync(FilterDefinition <T> Match, PagerInfo Info) { int pageindex = (Info.CurrenetPageIndex < 1) ? 1 : Info.CurrenetPageIndex; int pageSize = (Info.PageSize <= 0) ? 20 : Info.PageSize; int excludedRows = (pageindex - 1) * pageSize; IFindFluent <T, T> find = GetQueryable(Match); Info.RecordCount = (int)find.Count(); return(await find.Skip(excludedRows).Limit(pageSize).ToListAsync()); }
long IQuery <T> .Count() { IFindFluent <T, T> finder = null; var collection = _db.GetCollection <T>(typeof(T).Name); if (whereExp != null) { finder = collection.Find <T>(whereExp); } else { FilterDefinition <T> filter = FilterDefinition <T> .Empty; finder = collection.Find <T>(filter); } return(finder.Count()); }
public object count() { return(_documents.Count()); }
public long Count() { return(_fluentCursor.Count()); }
public Results <TEntity> Find(Expression <Func <TEntity, bool> > exp, int pageIndex, int pageSize, object sortBy = null) { IFindFluent <TEntity, TEntity> res = FindFluent(exp, pageIndex, pageSize, sortBy); return(new Results <TEntity>(res.ToList(), res.Count(), pageIndex, pageSize)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collection"></param> /// <param name="filter"></param> /// <param name="projection"></param> private void Apply <T>(IMongoCollection <T> collection, FilterDefinition <T> filter, ProjectionDefinition <T> projection) { filter = CreateFilters <T>(Filter, filter); if (IsAggregate) { var sortB = Builders <T> .Sort; SortDefinition <T> sort = null; foreach (var item in Order) { switch (item.Direction.ToString()) { case "-": { if (sort == null) { sort = sortB.Descending(item.Property); } else { sort = sort.Descending(item.Property); } break; } case "+": { if (sort == null) { sort = sortB.Ascending(item.Property); } else { sort = sort.Ascending(item.Property); } break; } } } var Group = BsonDocumentWrapper.Parse(JsonConvert.SerializeObject(GroupBy)); IAggregateFluent <T> query = null; var options = new AggregateOptions(); if (sort != null) { query = collection.Aggregate().Match(filter).Sort(sort).Group <T>(Group).Project <T>(BsonDocumentWrapper.Parse("{'id':'$_id','count':'$count'}")); } else { query = collection.Aggregate().Match(filter).Group <T>(Group).Project <T>(BsonDocumentWrapper.Parse("{'id':'$_id','count':'$count'}")); } Data = query.ToList <T>(); } else { IFindFluent <T, T> query = null; if (filter != null) { query = collection.Find(filter); } else { query = collection.Find(x => true); } if (projection != null) { query = query.Project <T>(projection); } var sortB = Builders <T> .Sort; SortDefinition <T> sort = null; foreach (var item in Order) { switch (item.Direction.ToString()) { case "-": { if (sort == null) { sort = sortB.Descending(item.Property); } else { sort = sort.Descending(item.Property); } break; } case "+": { if (sort == null) { sort = sortB.Ascending(item.Property); } else { sort = sort.Ascending(item.Property); } break; } } } if (sort != null) { query.Sort(sort); } PageSize = PageSize ?? 1; RecordCount = query.Count(); if (PageSize != int.MaxValue) { PageCount = (RecordCount / PageSize); PageCount += (PageCount < ((float)RecordCount / (float)PageSize)) ? 1 : 0; Data = query.Skip((PageIndex - 1) * PageSize).Limit(PageSize).ToList <T>(); } else { PageCount = 1; Data = query.ToList <T>(); } } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="E"></typeparam> public void Apply <T, E>() where T : class, IDto <E, T>, new() where E : class, IDocumentEntity, IEntity, new() { FilterDefinition <E> filter = (CreateFilter == null) ? null : (FilterDefinition <E>)CreateFilter?.Invoke(); ProjectionDefinition <E> projection = (CreateProjection == null) ? null : (ProjectionDefinition <E>)CreateProjection?.Invoke(); filter = CreateFilters <E>(Filter, filter); if (IsAggregate) { var query = MongoDbServices.Instance(IntanceMongoDb).GetCollection <E>().Aggregate().Match(filter).Group(projection); } else { IFindFluent <E, E> query = null; if (filter != null) { query = MongoDbServices.Instance(IntanceMongoDb).GetCollection <E>().Find(filter); } else { query = MongoDbServices.Instance(IntanceMongoDb).GetCollection <E>().Find(x => true); } if (projection != null) { query.Project(projection); } var sortB = Builders <E> .Sort; SortDefinition <E> sort = null; foreach (var item in Order) { switch (item.Direction.ToString()) { case "-": { if (sort == null) { sort = sortB.Descending(item.Property); } else { sort = sort.Descending(item.Property); } break; } case "+": { if (sort == null) { sort = sortB.Ascending(item.Property); } else { sort = sort.Ascending(item.Property); } break; } } } if (sort != null) { query.Sort(sort); } PageSize = PageSize ?? 1; RecordCount = query.Count(); PageCount = (RecordCount / PageSize); PageCount += (PageCount < ((float)RecordCount / (float)PageSize)) ? 1 : 0; Data = new T().SetEntity(query.Skip((PageIndex - 1) * PageSize).Limit(PageSize).ToList <E>()); } }