public static dynamic GetAutoQuery <TEntity>(string queryString, Type entityType, IQueryable <TEntity> query, AutoQueryableProfile profile) where TEntity : class
        {
            if (string.IsNullOrEmpty(queryString))
            {
                IEnumerable <string> columns = SelectHelper.GetSelectableColumns(profile?.UnselectableProperties, entityType);
                return(query.Select(SelectHelper.GetSelector <TEntity>(string.Join(",", columns.ToArray()))));
            }
            string[] queryStringParts = queryString.Replace("?", "").Split('&');

            IList <Criteria> criterias = CriteriaManager.GetCriterias(entityType, queryStringParts).ToList();
            IList <Clause>   clauses   = ClauseManager.GetClauses(queryStringParts).ToList();

            Clause wrapWithClause = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.WrapWith);
            var    countAllRows   = false;
            IEnumerable <WrapperPartType> wrapperParts = null;

            if (wrapWithClause != null)
            {
                wrapperParts = WrapperManager.GetWrapperParts(wrapWithClause.Value.Split(',')).ToList();
                countAllRows = wrapperParts.Contains(WrapperPartType.TotalCount);
            }

            QueryResult queryResult = QueryBuilder.Build(query, entityType, clauses, criterias, profile?.UnselectableProperties, countAllRows);

            if (wrapWithClause == null)
            {
                return(queryResult.Result);
            }

            return(WrapperManager.GetWrappedResult(wrapperParts, queryResult, clauses, queryString));
        }
Exemplo n.º 2
0
        public static QueryResult Build <T>(IQueryable <T> query, Type entityType, IList <Clause> clauses, IList <Criteria> criterias, string[] unselectableProperties, bool countAllRows) where T : class
        {
            Clause selectClause      = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.Select);
            Clause topClause         = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.Top);
            Clause skipClause        = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.Skip);
            Clause firstClause       = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.First);
            Clause lastClause        = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.Last);
            Clause orderByClause     = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.OrderBy);
            Clause orderByDescClause = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.OrderByDesc);
            Clause includeClause     = clauses.FirstOrDefault(c => c.ClauseType == ClauseType.Include);

            List <string>        selectColumns    = SelectHelper.GetSelectableColumns(selectClause, unselectableProperties, entityType).ToList();
            IEnumerable <Column> orderColumns     = OrderByHelper.GetOrderByColumns(orderByClause, unselectableProperties, entityType);
            IEnumerable <Column> orderDescColumns = OrderByHelper.GetOrderByColumns(orderByDescClause, unselectableProperties, entityType);

            if (criterias.Any())
            {
                query = query.Where(criterias);
            }
            var totalCount = 0;

            if (countAllRows)
            {
                totalCount = query.Count();
            }
            if (orderColumns != null)
            {
                query = query.OrderBy(orderColumns);
            }
            else if (orderDescColumns != null)
            {
                query = query.OrderByDesc(orderDescColumns);
            }

            if (includeClause != null)
            {
                List <string> includeColumns = IncludeHelper.GetIncludableColumns(includeClause, unselectableProperties, entityType).ToList();
                foreach (string column in includeColumns)
                {
                    query = query.Include(column);
                }
            }

            IQueryable <object> queryProjection;

            if (selectClause == null && unselectableProperties == null)
            {
                queryProjection = query;
            }
            else
            {
                queryProjection = query.Select(SelectHelper.GetSelector <T>(string.Join(",", selectColumns)));
            }

            if (skipClause != null)
            {
                int skip;
                int.TryParse(skipClause.Value, out skip);
                queryProjection = queryProjection.Skip(skip);
            }
            if (topClause != null)
            {
                int take;
                int.TryParse(topClause.Value, out take);
                queryProjection = queryProjection.Take(take);
            }
            else if (firstClause != null)
            {
                return(new QueryResult {
                    Result = queryProjection.FirstOrDefault(), TotalCount = totalCount
                });
            }
            else if (lastClause != null)
            {
                return(new QueryResult {
                    Result = queryProjection.LastOrDefault(), TotalCount = totalCount
                });
            }
            return(new QueryResult {
                Result = queryProjection, TotalCount = totalCount
            });
        }