Beispiel #1
0
        /// <summary>
        /// 带mapper
        /// </summary>
        /// <typeparam name="TEntityOrView"></typeparam>
        /// <typeparam name="TDto"></typeparam>
        /// <param name="query"></param>
        /// <param name="pagedInputDto"></param>
        /// <param name="configurationProvider"></param>
        /// <returns></returns>
        public static async Task <MyPagedResult <TDto> > GetPageAsync <TEntityOrView, TDto>(this IQueryable <TEntityOrView> query, PagedInputDto pagedInputDto) where TEntityOrView : class where TDto : class
        {
            query = EntityFrameworkQueryableExtensions.AsNoTracking <TEntityOrView>(query);
            //排序
            if (!string.IsNullOrEmpty(pagedInputDto.Order))
            {
                List <string> strList = pagedInputDto.Order.Split(new char[1]
                {
                    ','
                }).ToList();
                for (int i = 0; i < strList.Count; i++)
                {
                    query = ((i != 0) ? ((!strList[i].ToLower().Contains("desc")) ? DynamicQueryable.OrderBy <TEntityOrView>(query, pagedInputDto.Order, Array.Empty <object>()) : DynamicQueryable.ThenByDescending <TEntityOrView>(query, pagedInputDto.Order, Array.Empty <object>())) : ((!strList[i].ToLower().Contains("desc")) ? DynamicQueryable.OrderBy <TEntityOrView>(query, pagedInputDto.Order, Array.Empty <object>()) : DynamicQueryable.OrderByDescending <TEntityOrView>(query, pagedInputDto.Order, Array.Empty <object>())));
                }
            }
            //if (!string.IsNullOrEmpty(pagedInputDto.Order))
            //{
            //    query = DynamicQueryable.OrderBy<TEntityOrView>(query, pagedInputDto.Order, Array.Empty<object>());
            //}
            if (pagedInputDto.Filter != null)
            {
                string   text  = pagedInputDto.Filter.ToWhere();
                object[] array = pagedInputDto.Filter.paramValues.ToArray();
                query = DynamicQueryable.Where <TEntityOrView>(query, text, array);
            }
            IQueryable <TEntityOrView> queryable = query.Skip(pagedInputDto.SkipCount).Take(pagedInputDto.PageSize);

            IQueryable <TDto> queryable2;

            if (!string.IsNullOrEmpty(pagedInputDto.Select))
            {
                queryable2 = DynamicQueryable.Select(queryable, pagedInputDto.Select, Array.Empty <object>()).Cast <TDto>();
            }
            else
            {
                queryable2 = Extensions.ProjectTo <TDto>(queryable, pagedInputDto.configurationProvider, Array.Empty <Expression <Func <TDto, object> > >());
            }
            List <TDto> dataList = await EntityFrameworkQueryableExtensions.ToListAsync <TDto>(queryable2, default(CancellationToken));

            MyPagedResult <TDto> obj = new MyPagedResult <TDto>();

            obj.PageSize  = pagedInputDto.PageSize;
            obj.PageIndex = pagedInputDto.PageIndex;
            obj.DataList  = dataList;
            obj.RowCount  = query.Count();
            return(obj);
        }
Beispiel #2
0
        public DynamicQueryable BuildQuery(
            IQueryable queryable,
            IEnumerable <string> columns,
            NodeBase filter,
            IEnumerable <OrderItem> sort
            )
        {
            var query = new DynamicQueryable(queryable);

            //Get all predicate expressions
            var predicateMembers =
                filter != null ?
                new DynamicProjectionPredicateMemberVisitor().Visit(filter) :
                new List <string>();

            //Determine target of predicate (source or projection)
            var predicateTarget =
                predicateMembers.All(x => this.CompiledConfiguration.CompiledMembers.ContainsKey(x)) ?
                DynamicProjectionOperationTarget.Source : DynamicProjectionOperationTarget.Projection;

            //Determine target of sort (source or projection)
            var sortTarget =
                (sort ?? Enumerable.Empty <OrderItem>())
                .All(x => CompiledConfiguration.CompiledMembers.ContainsKey(x.Expression)) ?
                DynamicProjectionOperationTarget.Source : DynamicProjectionOperationTarget.Projection;

            //Apply predicate on source if applicable
            if (filter != null && predicateTarget == DynamicProjectionOperationTarget.Source)
            {
                query = query.Where(
                    new DynamicProjectionPredicateVisitor(this)
                    .VisitLambda(filter));
            }

            //Apply sort on source if applicable
            if (sort != null && sortTarget == DynamicProjectionOperationTarget.Source)
            {
                query = sort.Aggregate(query, (acc, next) =>
                {
                    var lambda =
                        Expression.Lambda(
                            CompiledConfiguration.CompiledMembers[next.Expression]
                            .Source.SourceExpression,
                            CompiledConfiguration.It);

                    return(next.IsDescending ?
                           query.OrderByDescending(lambda) :
                           query.OrderBy(lambda));
                });
            }

            //Append predicate and sort columns to projection if needed
            var selectorColumns = columns ?? this.CompiledConfiguration.CompiledMembers.Select(x => x.Value.Member.MemberInfo?.Name);

            if (predicateTarget == DynamicProjectionOperationTarget.Projection)
            {
                selectorColumns = columns.Concat(predicateMembers);
            }
            if (sortTarget == DynamicProjectionOperationTarget.Projection)
            {
                selectorColumns = columns.Concat(sort.Select(x => x.Expression));
            }

            selectorColumns = selectorColumns.Distinct().ToList();

            //Apply selector
            query = query.Select(compiler.BuildSelector(selectorColumns));

            //Apply predicate on projection if applicable
            if (filter != null && predicateTarget == DynamicProjectionOperationTarget.Projection)
            {
                query = query.Where(
                    new ExpressionNodeVisitor()
                    .VisitLambda(filter, configuration.ProjectedType));
            }

            //Apply sort on projection if applicable
            if (sort != null && predicateTarget == DynamicProjectionOperationTarget.Projection)
            {
                query.OrderBy(sort);
            }

            return(query);
        }