protected virtual IQueryExecutionResult <TRecord> ExecuteGrouping <TSource, TRecord>() { var result = new QueryExecutionGroupResult <TRecord>(); // preserve queryable. var queryableAfterFilters = CurrentQueryable; result.TotalRecords = queryableAfterFilters.LongCount(); CalculatePageCount(result); // intercept groups in advance to avoid doing it more than once :) var finalGroups = Criteria.Groups.Select(g => InterceptGroup <TSource>(g)).ToList(); // get the aggregates. var aggregateResults = FetchAggregates <TSource>(finalGroups); // sorting. finalGroups.ReversedForEach(fg => Criteria.Sorts.Insert(0, new Sort(fg.Path, fg.Ascending))); // apply sorting and paging. ApplySorting <TSource>(); ApplyPaging <TSource>(); if (Options.GroupByInMemory) { CurrentQueryable = CurrentQueryable.ToObjectList().Cast <TSource>().AsQueryable(); } CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => { gb.NullChecking(Options.GroupByInMemory ? Options.GroupByInMemoryNullCheck : false); finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}")); }); CurrentQueryable = CurrentQueryable.Select(sb => { sb.NullChecking(Options.GroupByInMemory ? Options.GroupByInMemoryNullCheck : false); finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}")); sb.ToList("Records"); }); // loop through the grouped records. var groupRecords = CurrentQueryable.ToDynamicClassList(); // now join them into logical collections var lastLists = new List <(List <TSource> source, IGroupQueryResult <TRecord> group)>(); result.Groups = RecursiveRegroup <TSource, TRecord>(groupRecords, aggregateResults, Criteria.Groups.First(), lastLists); // intercept grouped by. QueryInterceptToGrouped <TSource, TRecord>(lastLists).Wait(); result.Aggregates = CalculateTotalAggregate <TSource>(queryableAfterFilters); return(result); }
protected virtual async Task <IQueryExecutionResult <TRecord> > ExecuteAsyncGrouping <TSource, TRecord>(CancellationToken cancellationToken) { var result = new QueryExecutionGroupResult <TRecord>(); // preserve queryable. var queryableAfterFilters = CurrentQueryable; // async. result.TotalRecords = await this.AsyncQueryableService.LongCountAsync((IQueryable <TSource>) queryableAfterFilters, cancellationToken); CalculatePageCount(result); // intercept groups in advance to avoid doing it more than once :) var finalGroups = Criteria.Groups.Select(g => InterceptGroup <TSource>(g)).ToList(); // get the aggregates. var aggregateResults = await FetchAggregatesAsync <TSource>(finalGroups, cancellationToken); // sorting. finalGroups.ReversedForEach(fg => Criteria.Sorts.Insert(0, new Sort(fg.Path, fg.Ascending))); // apply sorting and paging. ApplySorting <TSource>(); ApplyPaging <TSource>(); // create group & select expression. CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}"))); CurrentQueryable = CurrentQueryable.Select(sb => { finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}")); sb.ToList("Records"); }); // loop through the grouped records. var groupRecords = await AsyncQueryableService.ToListAsync(CurrentQueryable.Cast <DynamicClass>(), cancellationToken); // now join them into logical collections var lastLists = new List <(List <TSource> entities, IGroupQueryResult <TRecord> group)>(); result.Groups = RecursiveRegroup <TSource, TRecord>(groupRecords, aggregateResults, Criteria.Groups.First(), lastLists); // converted to grouped by. await QueryInterceptToGrouped <TSource, TRecord>(lastLists); result.Aggregates = await CalculateTotalAggregateAsync <TSource>(queryableAfterFilters, cancellationToken); return(result); }
protected virtual IQueryExecutionResult ExecuteGrouping <T>() { var result = new QueryExecutionResult(); // preserve queryable. var queryableAfterFilters = CurrentQueryable; result.TotalRecords = queryableAfterFilters.LongCount(); CalculatePageCount(result); // intercept groups in advance to avoid doing it more than once :) var finalGroups = Criteria.Groups.Select(g => InterceptGroup <T>(g)).ToList(); // get the aggregates. var aggregateResults = FetchAggregates <T>(finalGroups); // sorting. finalGroups.ForEach(fg => Criteria.Sorts.Insert(0, new Sort(fg.Path, fg.Ascending))); // apply sorting and paging. ApplySorting <T>(); ApplyPaging <T>(); // create group & select expression. CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}"))); CurrentQueryable = CurrentQueryable.Select(sb => { finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}")); sb.ToList("Records"); }); // loop through the grouped records. // var groupRecords = CurrentQueryable.ToDynamicClassList(); // now join them into logical collections // result.Data = RecursiveRegroup<T>(groupRecords, aggregateResults, Criteria.Groups.First()); result.Data = CurrentQueryable; result.Aggregates = CalculateTotalAggregate <T>(CurrentQueryable); return(result); }
protected virtual IQueryable CreateFetchAggregateSelectExpression <TSource>(IGroup finalGroup, List <IGroup> previousGroups) { var groupExpression = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => { var groupKeyIndex = -1; previousGroups.ForEach(pg => gb.Path(pg.Path, $"Key_{++groupKeyIndex}")); gb.Path(finalGroup.Path, $"Key_{++groupKeyIndex}"); }); var selectExpression = groupExpression.Select(sb => { var groupKeyIndex = -1; previousGroups.ForEach(pg => sb.Key($"Key_{++groupKeyIndex}", $"Key_{groupKeyIndex}")); sb.Key($"Key_{++groupKeyIndex}", $"Key_{groupKeyIndex}"); Criteria.Aggregates.ForEach((a, ai) => { var fa = InterceptAggregate <TSource>(a); var selectType = ResolveSelectFrom(fa.Type); sb.Aggregate(fa.Path, selectType, $"Agg_{ai}"); }); }); return(selectExpression); }