grouping result is ready, next step is to flatten the aggregates that match.
This commit is contained in:
		
							parent
							
								
									01de3b480b
								
							
						
					
					
						commit
						4bb96b2058
					
				@ -79,7 +79,7 @@ namespace PoweredSoft.DynamicQuery.Cli
 | 
			
		||||
            var queryable = list.AsQueryable();
 | 
			
		||||
            var criteria = new QueryCriteria();
 | 
			
		||||
            criteria.Page = 1;
 | 
			
		||||
            criteria.PageSize = 2;
 | 
			
		||||
            criteria.PageSize = 10;
 | 
			
		||||
 | 
			
		||||
            criteria.Groups = new List<IGroup>()
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
@ -24,8 +24,9 @@ namespace PoweredSoft.DynamicQuery
 | 
			
		||||
 | 
			
		||||
        protected virtual IQueryExecutionResult ExecuteGrouping<T>()
 | 
			
		||||
        {
 | 
			
		||||
            var result = new GroupedQueryExecutionResult();
 | 
			
		||||
            var result = new QueryExecutionResult();
 | 
			
		||||
            result.TotalRecords = CurrentQueryable.LongCount();
 | 
			
		||||
            CalculatePageCount(result);
 | 
			
		||||
 | 
			
		||||
            // intercept groups in advance to avoid doing it more than once :)
 | 
			
		||||
            var finalGroups = Criteria.Groups.Select(g => InterceptGroup<T>(g)).ToList();
 | 
			
		||||
@ -48,7 +49,7 @@ namespace PoweredSoft.DynamicQuery
 | 
			
		||||
                    {
 | 
			
		||||
                        var groupKeyIndex = -1;
 | 
			
		||||
                        previousGroups.ForEach(pg => sb.Key($"Key_{++groupKeyIndex}"));
 | 
			
		||||
                        sb.Key($"Key_{++groupKeyIndex}");
 | 
			
		||||
                        sb.Key($"Key_{++groupKeyIndex}", $"Key_{groupKeyIndex}");
 | 
			
		||||
                        Criteria.Aggregates.ForEach(a =>
 | 
			
		||||
                        {
 | 
			
		||||
                            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.Select(sb =>
 | 
			
		||||
            {
 | 
			
		||||
                finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}"));
 | 
			
		||||
                finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}"));
 | 
			
		||||
                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;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        protected virtual IQueryExecutionResult ExecuteNoGrouping<T>()
 | 
			
		||||
        {
 | 
			
		||||
            var result = new QueryExecutionResult();
 | 
			
		||||
 | 
			
		||||
            // total records.
 | 
			
		||||
            result.TotalRecords = CurrentQueryable.LongCount();
 | 
			
		||||
            CalculatePageCount(result);
 | 
			
		||||
 | 
			
		||||
            // sorts and paging.
 | 
			
		||||
            ApplySorting<T>();
 | 
			
		||||
@ -106,14 +134,6 @@ namespace PoweredSoft.DynamicQuery
 | 
			
		||||
            // the data.
 | 
			
		||||
            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;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
        {
 | 
			
		||||
            var original = new List<ISort>()
 | 
			
		||||
 | 
			
		||||
@ -25,26 +25,17 @@ namespace PoweredSoft.DynamicQuery
 | 
			
		||||
        public bool ShouldSerializeAggregates() => Aggregates != null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // not grouped.
 | 
			
		||||
    // just result
 | 
			
		||||
    public class QueryExecutionResult : QueryResult, IQueryExecutionResult
 | 
			
		||||
    {
 | 
			
		||||
        public long TotalRecords { get; set; }
 | 
			
		||||
        public long? NumberOfPages { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // grouped.
 | 
			
		||||
    // group result.
 | 
			
		||||
    public class GroupQueryResult : QueryResult, IGroupQueryResult
 | 
			
		||||
    {
 | 
			
		||||
        public string GroupPath { 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; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user