grouping result is ready, next step is to flatten the aggregates that match.

This commit is contained in:
David Lebee 2018-10-21 14:15:26 -05:00
parent 01de3b480b
commit 4bb96b2058
4 changed files with 48 additions and 25 deletions

View File

@ -79,7 +79,7 @@ namespace PoweredSoft.DynamicQuery.Cli
var queryable = list.AsQueryable(); var queryable = list.AsQueryable();
var criteria = new QueryCriteria(); var criteria = new QueryCriteria();
criteria.Page = 1; criteria.Page = 1;
criteria.PageSize = 2; criteria.PageSize = 10;
criteria.Groups = new List<IGroup>() criteria.Groups = new List<IGroup>()
{ {

View File

@ -24,8 +24,9 @@ namespace PoweredSoft.DynamicQuery
protected virtual IQueryExecutionResult ExecuteGrouping<T>() protected virtual IQueryExecutionResult ExecuteGrouping<T>()
{ {
var result = new GroupedQueryExecutionResult(); var result = new QueryExecutionResult();
result.TotalRecords = CurrentQueryable.LongCount(); result.TotalRecords = CurrentQueryable.LongCount();
CalculatePageCount(result);
// intercept groups in advance to avoid doing it more than once :) // intercept groups in advance to avoid doing it more than once :)
var finalGroups = Criteria.Groups.Select(g => InterceptGroup<T>(g)).ToList(); var finalGroups = Criteria.Groups.Select(g => InterceptGroup<T>(g)).ToList();
@ -48,7 +49,7 @@ namespace PoweredSoft.DynamicQuery
{ {
var groupKeyIndex = -1; var groupKeyIndex = -1;
previousGroups.ForEach(pg => sb.Key($"Key_{++groupKeyIndex}")); previousGroups.ForEach(pg => sb.Key($"Key_{++groupKeyIndex}"));
sb.Key($"Key_{++groupKeyIndex}"); sb.Key($"Key_{++groupKeyIndex}", $"Key_{groupKeyIndex}");
Criteria.Aggregates.ForEach(a => Criteria.Aggregates.ForEach(a =>
{ {
var selectType = ResolveSelectFrom(a.Type); var selectType = ResolveSelectFrom(a.Type);
@ -80,24 +81,51 @@ namespace PoweredSoft.DynamicQuery
CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}"))); CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}")));
CurrentQueryable = CurrentQueryable.Select(sb => CurrentQueryable = CurrentQueryable.Select(sb =>
{ {
finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}")); finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}"));
sb.ToList("Records"); sb.ToList("Records");
}); });
var temp = CurrentQueryable.ToDynamicClassList(); var groupRecords = CurrentQueryable.ToDynamicClassList();
result.Data = groupRecords.Select((groupRecord, groupRecordIndex) =>
{
var groupRecordResult = new GroupQueryResult();
GroupQueryResult previous = null;
Criteria.Groups.ForEach((g, gi) =>
{
bool isFirst = gi == 0;
bool isLast = Criteria.Groups.Count - 1 == gi;
var cgrr = isFirst ? groupRecordResult : new GroupQueryResult();
cgrr.GroupPath = g.Path;
cgrr.GroupValue = groupRecord.GetDynamicPropertyValue($"Key_{gi}");
if (!isLast)
cgrr.Data = new List<object>();
else
cgrr.Data = groupRecord.GetDynamicPropertyValue<List<T>>("Records").Cast<object>().ToList();
if (previous != null)
previous.Data.Add(cgrr);
previous = cgrr;
});
return (object)groupRecordResult;
}).ToList();
return result; return result;
} }
protected virtual IQueryExecutionResult ExecuteNoGrouping<T>() protected virtual IQueryExecutionResult ExecuteNoGrouping<T>()
{ {
var result = new QueryExecutionResult(); var result = new QueryExecutionResult();
// total records. // total records.
result.TotalRecords = CurrentQueryable.LongCount(); result.TotalRecords = CurrentQueryable.LongCount();
CalculatePageCount(result);
// sorts and paging. // sorts and paging.
ApplySorting<T>(); ApplySorting<T>();
@ -106,14 +134,6 @@ namespace PoweredSoft.DynamicQuery
// the data. // the data.
result.Data = CurrentQueryable.ToObjectList(); result.Data = CurrentQueryable.ToObjectList();
// if there is paging.
if (HasPaging)
{
if (result.TotalRecords < Criteria.PageSize)
result.NumberOfPages = 1;
else
result.NumberOfPages = result.TotalRecords / Criteria.PageSize + (result.TotalRecords % Criteria.PageSize != 0 ? 1 : 0);
}
return result; return result;
} }

View File

@ -75,6 +75,18 @@ namespace PoweredSoft.DynamicQuery
}); });
} }
protected virtual void CalculatePageCount(IQueryExecutionResult result)
{
if (!HasPaging)
return;
if (result.TotalRecords < Criteria.PageSize)
result.NumberOfPages = 1;
else
result.NumberOfPages = result.TotalRecords / Criteria.PageSize + (result.TotalRecords % Criteria.PageSize != 0 ? 1 : 0);
}
protected virtual List<ISort> InterceptSort<T>(ISort sort) protected virtual List<ISort> InterceptSort<T>(ISort sort)
{ {
var original = new List<ISort>() var original = new List<ISort>()

View File

@ -25,26 +25,17 @@ namespace PoweredSoft.DynamicQuery
public bool ShouldSerializeAggregates() => Aggregates != null; public bool ShouldSerializeAggregates() => Aggregates != null;
} }
// not grouped. // just result
public class QueryExecutionResult : QueryResult, IQueryExecutionResult public class QueryExecutionResult : QueryResult, IQueryExecutionResult
{ {
public long TotalRecords { get; set; } public long TotalRecords { get; set; }
public long? NumberOfPages { get; set; } public long? NumberOfPages { get; set; }
} }
// grouped. // group result.
public class GroupQueryResult : QueryResult, IGroupQueryResult public class GroupQueryResult : QueryResult, IGroupQueryResult
{ {
public string GroupPath { get; set; } public string GroupPath { get; set; }
public object GroupValue { get; set; } public object GroupValue { get; set; }
public IEnumerable<IQueryResult> GroupItems => Data.Cast<IQueryResult>();
public bool ShouldSerializeGroupItems() => false;
}
public class GroupedQueryExecutionResult : GroupQueryResult, IQueryExecutionResult
{
public long TotalRecords { get; set; }
public long? NumberOfPages { get; set; }
} }
} }