Example #1
0
        /// <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>());
            }
        }