pieces are ready to connect :P

This commit is contained in:
David Lebee 2018-10-21 13:15:21 -05:00
parent 8ca897b304
commit 244130c4cb
3 changed files with 71 additions and 42 deletions

View File

@ -48,6 +48,7 @@ namespace PoweredSoft.DynamicQuery.Cli
public string FirstName { get; set; } public string FirstName { get; set; }
public string LastName { get; set; } public string LastName { get; set; }
public int Age { get; set; } public int Age { get; set; }
public string Sexe { get; set; }
} }
public class OtherClass public class OtherClass
@ -68,11 +69,11 @@ namespace PoweredSoft.DynamicQuery.Cli
{ {
var list = new List<Person>() var list = new List<Person>()
{ {
new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Age = 29}, new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Sexe = "Male", Age = 29},
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Age = 29}, new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sexe = "Female", Age = 29},
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Age = 20}, new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sexe = "Female", Age = 20},
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Age = 30}, new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sexe = "Male", Age = 30},
new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Age = 30}, new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Sexe = "Female", Age = 30},
}; };
var queryable = list.AsQueryable(); var queryable = list.AsQueryable();
@ -80,30 +81,15 @@ namespace PoweredSoft.DynamicQuery.Cli
criteria.Page = 1; criteria.Page = 1;
criteria.PageSize = 10; criteria.PageSize = 10;
/*
criteria.Filters = new List<IFilter>
{
new SimpleFilter() {Path = nameof(Person.LastName), Value = "Lebee", Type = FilterType.Equal},
new CompositeFilter()
{
Type = FilterType.Composite,
And = true,
Filters = new List<IFilter>
{
new SimpleFilter() {Path = nameof(Person.FirstName), Value = "David", Type = FilterType.Equal},
new SimpleFilter() {Path = nameof(Person.FirstName), Value = "Zohra", Type = FilterType.Equal},
}
}
};*/
criteria.Groups = new List<IGroup>() criteria.Groups = new List<IGroup>()
{ {
new Group { Path = "LastName" } new Group { Path = "LastName" },
new Group { Path = "Sexe" }
}; };
criteria.Aggregates = new List<IAggregate>() criteria.Aggregates = new List<IAggregate>()
{ {
new Aggregate { Path = "Age", Type = AggregateType.Count }, new Aggregate { Type = AggregateType.Count },
new Aggregate { Path = "Age", Type = AggregateType.Avg } new Aggregate { Path = "Age", Type = AggregateType.Avg }
};; };;

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using PoweredSoft.DynamicLinq; using PoweredSoft.DynamicLinq;
using PoweredSoft.DynamicLinq.Fluent;
using PoweredSoft.DynamicQuery.Core; using PoweredSoft.DynamicQuery.Core;
namespace PoweredSoft.DynamicQuery namespace PoweredSoft.DynamicQuery
@ -26,32 +27,74 @@ namespace PoweredSoft.DynamicQuery
var result = new GroupedQueryExecutionResult(); var result = new GroupedQueryExecutionResult();
result.TotalRecords = CurrentQueryable.LongCount(); result.TotalRecords = CurrentQueryable.LongCount();
Criteria.Groups.ForEach(group => // intercept groups in advance to avoid doing it more than once :)
{ var finalGroups = Criteria.Groups.Select(g => InterceptGroup<T>(g)).ToList();
var finalGroup = InterceptGroup<T>(group);
var groupCleanedPath = group.Path.Replace(".", "");
CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb =>
{
gb.Path(finalGroup.Path);
});
CurrentQueryable = CurrentQueryable.Select(sb => // get the aggregates.
List<List<DynamicClass>> aggregateResults = null;
if (Criteria.Aggregates.Any())
{
var previousGroups = new List<IGroup>();
aggregateResults = finalGroups.Select(fg =>
{ {
sb.ToList("Data"); var groupExpression = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb =>
sb.Key($"Group_{groupCleanedPath}", group.Path);
Criteria.Aggregates.ForEach(a =>
{ {
var selectType = ResolveSelectFrom(a.Type); previousGroups.ForEach(pg =>
var pathCleaned = a.Path.Replace(".", ""); {
sb.Aggregate(a.Path, selectType, $"Agg_{a.Type}_{pathCleaned}"); var previousGroupCleanedPath = pg.Path.Replace(".", "");
gb.Path(pg.Path, $"Key_{previousGroupCleanedPath}");
});
var cleanedPath = fg.Path.Replace(".", "");
gb.Path(fg.Path, $"Key_{cleanedPath}");
}); });
var selectExpression = groupExpression.Select(sb =>
{
previousGroups.ForEach(pg => sb.Key(pg.Path));
sb.Key(fg.Path);
Criteria.Aggregates.ForEach(a =>
{
var selectType = ResolveSelectFrom(a.Type);
var pathCleaned = a.Path?.Replace(".", "");
sb.Aggregate(a.Path, selectType, $"Agg_{a.Type}_{pathCleaned}");
});
});
var aggregateResult = selectExpression.ToDynamicClassList();
previousGroups.Add(fg);
return aggregateResult;
}).ToList();
}
// sorting.
finalGroups.ForEach(fg =>
{
Criteria.Sorts.Insert(0, new Sort()
{
Path = fg.Path,
Ascending = fg.Ascending
}); });
}); });
ApplySorting<T>();
ApplyPaging<T>();
// now get the data grouped.
CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach(fg => gb.Path(fg.Path)));
CurrentQueryable = CurrentQueryable.Select(sb =>
{
finalGroups.ForEach(fg => sb.Key(fg.Path));
sb.ToList("Records");
});
var temp = CurrentQueryable.ToDynamicClassList();
return result; return result;
} }
protected virtual IQueryExecutionResult ExecuteNoGrouping<T>() protected virtual IQueryExecutionResult ExecuteNoGrouping<T>()
{ {
var result = new QueryExecutionResult(); var result = new QueryExecutionResult();
@ -60,8 +103,8 @@ namespace PoweredSoft.DynamicQuery
result.TotalRecords = CurrentQueryable.LongCount(); result.TotalRecords = CurrentQueryable.LongCount();
// sorts and paging. // sorts and paging.
ApplyNoGroupingSorts<T>(); ApplySorting<T>();
ApplyNoGroupingPaging<T>(); ApplyPaging<T>();
// the data. // the data.
result.Data = CurrentQueryable.ToObjectList(); result.Data = CurrentQueryable.ToObjectList();

View File

@ -49,7 +49,7 @@ namespace PoweredSoft.DynamicQuery
} }
protected virtual void ApplyNoGroupingPaging<T>() protected virtual void ApplyPaging<T>()
{ {
if (!HasPaging) if (!HasPaging)
return; return;
@ -59,7 +59,7 @@ namespace PoweredSoft.DynamicQuery
CurrentQueryable = q.Skip(skip).Take(Criteria.PageSize.Value); CurrentQueryable = q.Skip(skip).Take(Criteria.PageSize.Value);
} }
protected virtual void ApplyNoGroupingSorts<T>() protected virtual void ApplySorting<T>()
{ {
if (Criteria.Sorts?.Any() != true) if (Criteria.Sorts?.Any() != true)
{ {