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)); }
public static dynamic GetAutoQuery <TEntity>(string queryString, Type entityType, IQueryable <TEntity> query, AutoQueryableProfile profile) where TEntity : class { // If query string empty select default entity properties. if (string.IsNullOrEmpty(queryString)) { IColumnProvider columnProvider = ProviderFactory.GetColumnProvider(); IEnumerable <SelectColumn> selectColumns = EntityColumnHelper.GetSelectableColumns(profile, entityType); if (profile.UseBaseType) { return(query.Select(SelectHelper.GetSelector <TEntity, TEntity>(selectColumns, profile))); } return(query.Select(SelectHelper.GetSelector <TEntity, object>(selectColumns, profile))); } // Get criteria & clauses from choosen provider (AQ, OData, ...) string[] queryStringParts = queryString.GetParts(); ICriteriaProvider criteriaProvider = ProviderFactory.GetCriteriaProvider(profile?.ProviderType); IList <Criteria> criterias = profile.IsClauseAllowed(ClauseType.Filter) ? criteriaProvider.GetCriterias(entityType, queryStringParts, profile).ToList() : null; IClauseProvider clauseProvider = ProviderFactory.GetClauseProvider(profile?.ProviderType); Clauses clauses = clauseProvider.GetClauses(queryStringParts, profile); var countAllRows = false; IEnumerable <WrapperPartType> wrapperParts = null; if (clauses.WrapWith != null) { IWrapperProvider wrapperProvider = ProviderFactory.GetWrapperProvider(); wrapperParts = wrapperProvider.GetWrapperParts(clauses.WrapWith.Value.Split(','), profile).ToList(); countAllRows = wrapperParts.Contains(WrapperPartType.TotalCount); } QueryResult queryResult = QueryBuilder.Build(query, entityType, clauses, criterias, profile, countAllRows); if (clauses.WrapWith == null || !wrapperParts.Any()) { return(queryResult.Result); } return(DefaultWrapperProvider.GetWrappedResult(wrapperParts, queryResult, clauses, queryString)); }
public static 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) { 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)); }
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 }); }
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 }); }