aggregates on all completed :)

This commit is contained in:
David Lebee 2018-10-21 16:06:45 -05:00
parent 94d241590e
commit d598508ab2
2 changed files with 42 additions and 7 deletions

View File

@ -83,8 +83,8 @@ namespace PoweredSoft.DynamicQuery.Cli
criteria.Groups = new List<IGroup>() criteria.Groups = new List<IGroup>()
{ {
new Group { Path = "LastName" }, new Group { Path = "LastName" }
new Group { Path = "Sexe" } //, new Group { Path = "Sexe" }
}; };
criteria.Aggregates = new List<IAggregate>() criteria.Aggregates = new List<IAggregate>()

View File

@ -25,7 +25,11 @@ namespace PoweredSoft.DynamicQuery
protected virtual IQueryExecutionResult ExecuteGrouping<T>() protected virtual IQueryExecutionResult ExecuteGrouping<T>()
{ {
var result = new QueryExecutionResult(); var result = new QueryExecutionResult();
result.TotalRecords = CurrentQueryable.LongCount();
// preserve queryable.
var queryableAfterFilters = CurrentQueryable;
result.TotalRecords = queryableAfterFilters.LongCount();
CalculatePageCount(result); CalculatePageCount(result);
// intercept groups in advance to avoid doing it more than once :) // intercept groups in advance to avoid doing it more than once :)
@ -64,8 +68,6 @@ namespace PoweredSoft.DynamicQuery
cgrr.GroupPath = g.Path; cgrr.GroupPath = g.Path;
cgrr.GroupValue = groupRecord.GetDynamicPropertyValue($"Key_{gi}"); cgrr.GroupValue = groupRecord.GetDynamicPropertyValue($"Key_{gi}");
if (!isLast) if (!isLast)
cgrr.Data = new List<object>(); cgrr.Data = new List<object>();
else else
@ -100,9 +102,39 @@ namespace PoweredSoft.DynamicQuery
return (object)groupRecordResult; return (object)groupRecordResult;
}).ToList(); }).ToList();
result.Aggregates = CalculateTotalAggregate(queryableAfterFilters);
return result; return result;
} }
protected virtual List<IAggregateResult> CalculateTotalAggregate(IQueryable queryableAfterFilters)
{
if (!Criteria.Aggregates.Any())
return null;
var groupExpression = queryableAfterFilters.EmptyGroupBy(QueryableUnderlyingType);
var selectExpression = groupExpression.Select(sb =>
{
Criteria.Aggregates.ForEach((a, index) =>
{
var selectType = ResolveSelectFrom(a.Type);
sb.Aggregate(a.Path, selectType, $"Agg_{index}");
});
});
var aggregateResult = selectExpression.ToDynamicClassList().First();
var ret = new List<IAggregateResult>();
Criteria.Aggregates.ForEach((a, index) =>
{
ret.Add(new AggregateResult()
{
Path = a.Path,
Type = a.Type,
Value = aggregateResult.GetDynamicPropertyValue($"Agg_{index}")
});
});
return ret;
}
private DynamicClass FindMatchingAggregateResult(List<List<DynamicClass>> aggregateResults, List<IGroup> groups, List<GroupQueryResult> groupResults) private DynamicClass FindMatchingAggregateResult(List<List<DynamicClass>> aggregateResults, List<IGroup> groups, List<GroupQueryResult> groupResults)
{ {
var groupIndex = groupResults.Count - 1; var groupIndex = groupResults.Count - 1;
@ -159,8 +191,11 @@ namespace PoweredSoft.DynamicQuery
{ {
var result = new QueryExecutionResult(); var result = new QueryExecutionResult();
// after filter queryable
var afterFilterQueryable = CurrentQueryable;
// total records. // total records.
result.TotalRecords = CurrentQueryable.LongCount(); result.TotalRecords = afterFilterQueryable.LongCount();
CalculatePageCount(result); CalculatePageCount(result);
// sorts and paging. // sorts and paging.
@ -169,7 +204,7 @@ namespace PoweredSoft.DynamicQuery
// the data. // the data.
result.Data = CurrentQueryable.ToObjectList(); result.Data = CurrentQueryable.ToObjectList();
result.Aggregates = CalculateTotalAggregate(afterFilterQueryable);
return result; return result;
} }