Example #1
0
        public static QueryResult Build <T>(IQueryable <T> query, Type entityType, Clauses clauses, IList <Criteria> criterias, AutoQueryableProfile profile, bool countAllRows) where T : class
        {
            IColumnProvider            columnProvider = ProviderFactory.GetColumnProvider(profile?.ProviderType);
            IEnumerable <SelectColumn> selectColumns  = columnProvider.GetSelectableColumns(clauses, profile, entityType);

            IEnumerable <Column> orderColumns     = OrderByHelper.GetOrderByColumns(profile, clauses.OrderBy, entityType);
            IEnumerable <Column> orderDescColumns = OrderByHelper.GetOrderByColumns(profile, clauses.OrderByDesc, entityType);

            if (criterias != null && 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);
            }

            IQueryable <object> queryProjection;

            if (clauses.Select == null && profile?.UnselectableProperties == null && profile?.SelectableProperties == null)
            {
                queryProjection = query;
            }
            else
            {
                if (profile.UseBaseType)
                {
                    queryProjection = query.Select(SelectHelper.GetSelector <T, T>(selectColumns, profile));
                }
                else
                {
                    queryProjection = query.Select(SelectHelper.GetSelector <T, object>(selectColumns, profile));
                }
            }

            if (clauses.Skip != null)
            {
                int.TryParse(clauses.Skip.Value, out int skip);
                if (profile?.MaxToSkip != null && skip > profile.MaxToSkip)
                {
                    skip = profile.MaxToSkip.Value;
                }
                queryProjection = queryProjection.Skip(skip);
            }
            if (clauses.Top != null)
            {
                int.TryParse(clauses.Top.Value, out int take);
                if (profile?.MaxToTake != null && take > profile?.MaxToTake)
                {
                    take = profile.MaxToTake.Value;
                }
                queryProjection = queryProjection.Take(take);
            }
            else if (clauses.First != null)
            {
                return(new QueryResult {
                    Result = queryProjection.FirstOrDefault(), TotalCount = totalCount
                });
            }
            else if (clauses.Last != null)
            {
                return(new QueryResult {
                    Result = queryProjection.LastOrDefault(), TotalCount = totalCount
                });
            }
            else if (profile?.MaxToTake != null)
            {
                queryProjection = queryProjection.Take(profile.MaxToTake.Value);
            }
            return(new QueryResult {
                Result = queryProjection, TotalCount = totalCount
            });
        }
Example #2
0
        public static IQueryable <dynamic> Build <T>(IClauseValueManager clauseValueManager, ICriteriaFilterManager criteriaFilterManager, IQueryable <T> query, ICollection <Criteria> criterias, IAutoQueryableProfile profile) where T : class
        {
            if (criterias != null && criterias.Any())
            {
                query = _addCriterias(criteriaFilterManager, query, criterias);
            }
            query = _addOrderBy(query, clauseValueManager.OrderBy, profile);

            TotalCountQuery = query;
            if (clauseValueManager.First)
            {
                query = query.Take(1);
            }
            else
            {
                query = _handlePaging(clauseValueManager, query, profile);
            }
            //if (profile?.MaxToTake != null)
            //{
            //    queryProjection = profile.UseBaseType ? ((IQueryable<T>)queryProjection).Take(profile.MaxToTake.Value) : queryProjection.Take(profile.MaxToTake.Value);
            //}

            IQueryable <dynamic> queryProjection = query;

            if (clauseValueManager.Select.Any() || profile?.UnselectableProperties != null || profile?.SelectableProperties != null)
            {
                if (profile != null)
                {
                    if (profile.ToListBeforeSelect)
                    {
                        query = query.ToList().AsQueryable();
                    }
                    queryProjection = profile.UseBaseType ?
                                      query.Select(SelectHelper.GetSelector <T, T>(clauseValueManager.Select, profile)) : query.Select(SelectHelper.GetSelector <T, object>(clauseValueManager.Select, profile));
                }
            }

            return(queryProjection);
        }
        public static dynamic Build <T>(IClauseValueManager clauseValueManager, IQueryable <T> query, IAutoQueryableProfile profile) where T : class
        {
            var totalCountQuery = query;

            query = _addOrderBy(query, clauseValueManager.OrderBy, profile);

            if (clauseValueManager.First)
            {
                return(query.FirstOrDefault());
            }
            if (clauseValueManager.Last)
            {
                return(query.LastOrDefault());
            }
            query = _handlePaging(clauseValueManager, query, profile);
            //if (profile?.MaxToTake != null)
            //{
            //    queryProjection = profile.UseBaseType ? ((IQueryable<T>)queryProjection).Take(profile.MaxToTake.Value) : queryProjection.Take(profile.MaxToTake.Value);
            //}

            IQueryable <dynamic> queryProjection = query;

            if (clauseValueManager.Select.Any() || profile?.UnselectableProperties != null || profile?.SelectableProperties != null)
            {
                if (profile != null)
                {
                    if (profile.ToListBeforeSelect)
                    {
                        query = query.ToList().AsQueryable();
                    }
                    queryProjection = profile.UseBaseType ?
                                      query.Select(SelectHelper.GetSelector <T, T>(clauseValueManager.Select, profile)) : query.Select(SelectHelper.GetSelector <T, object>(clauseValueManager.Select, profile));
                }
            }

            return(queryProjection.HandleWrapping(clauseValueManager, totalCountQuery));
        }