added support for lots of new aggregate helpers.

first breaking change but I think people will be okay with it :)
This commit is contained in:
David Lebee
2018-10-25 21:01:44 -05:00
parent dcaf114a09
commit 695d3085d1
5 changed files with 84 additions and 29 deletions
+1 -2
View File
@@ -49,14 +49,13 @@ namespace PoweredSoft.DynamicLinq
Sum,
Average,
ToList,
PathToList,
Path,
Min,
Max,
LastOrDefault,
FirstOrDefault,
Last,
First
First,
}
public enum SelectCollectionHandling
@@ -69,17 +69,28 @@ namespace PoweredSoft.DynamicLinq.Fluent
public SelectBuilder LongCount(string propertyName) => Aggregate(null, SelectTypes.LongCount, propertyName);
public SelectBuilder Sum(string path, string propertyName = null) => Aggregate(path, SelectTypes.Sum, propertyName);
public SelectBuilder Average(string path, string propertyName = null) => Aggregate(path, SelectTypes.Average, propertyName);
public void Min(string path, string propertyName = null) => Aggregate(path, SelectTypes.Min, propertyName);
public void Max(string path, string propertyName = null) => Aggregate(path, SelectTypes.Max, propertyName);
public SelectBuilder Min(string path, string propertyName = null) => Aggregate(path, SelectTypes.Min, propertyName);
public SelectBuilder Max(string path, string propertyName = null) => Aggregate(path, SelectTypes.Max, propertyName);
public SelectBuilder ToList(string propertyName) => Aggregate(null, SelectTypes.ToList, propertyName);
public SelectBuilder LastOrDefault(string propertyName) => Aggregate(null, SelectTypes.LastOrDefault, propertyName);
public SelectBuilder FirstOrDefault(string propertyName) => Aggregate(null, SelectTypes.FirstOrDefault, propertyName);
public SelectBuilder Last(string propertyName) => Aggregate(null, SelectTypes.Last, propertyName);
public SelectBuilder First(string propertyName) => Aggregate(null, SelectTypes.First, propertyName);
public SelectBuilder PathToList(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
=> Aggregate(path, SelectTypes.PathToList, propertyName: propertyName, selectCollectionHandling: selectCollectionHandling);
public SelectBuilder ToList(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
=> Aggregate(path, SelectTypes.ToList, propertyName: propertyName, selectCollectionHandling: selectCollectionHandling);
public void LastOrDefault(string propertyName) => Aggregate(null, SelectTypes.LastOrDefault, propertyName);
public void FirstOrDefault(string propertyName) => Aggregate(null, SelectTypes.FirstOrDefault, propertyName);
public void Last(string propertyName) => Aggregate(null, SelectTypes.Last, propertyName);
public void First(string propertyName) => Aggregate(null, SelectTypes.First, propertyName);
public SelectBuilder First(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
=> Aggregate(path, SelectTypes.First, propertyName: propertyName, selectCollectionHandling: selectCollectionHandling);
public SelectBuilder FirstOrDefault(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
=> Aggregate(path, SelectTypes.FirstOrDefault, propertyName: propertyName, selectCollectionHandling: selectCollectionHandling);
public SelectBuilder Last(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
=> Aggregate(path, SelectTypes.Last, propertyName: propertyName, selectCollectionHandling: selectCollectionHandling);
public SelectBuilder LastOrDefault(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
=> Aggregate(path, SelectTypes.LastOrDefault, propertyName: propertyName, selectCollectionHandling: selectCollectionHandling);
public virtual IQueryable Build()
{
@@ -246,25 +246,37 @@ namespace PoweredSoft.DynamicLinq.Helpers
resolver.Resolve();
return resolver.GetResultBodyExpression();
}
else if (selectType == SelectTypes.PathToList)
{
var parser = new ExpressionParser(parameter, path);
var resolver = new PathExpressionResolver(parser);
resolver.NullChecking = nullChecking;
resolver.CollectionHandling = selectCollectionHandling;
resolver.Resolve();
var expr = (resolver.Result as LambdaExpression).Body;
var notGroupedType = expr.Type.GenericTypeArguments.FirstOrDefault();
if (notGroupedType == null)
throw new Exception($"Path must be a Enumerable<T> but its a {expr.Type}");
else if (selectType == SelectTypes.ToList)
return CreateSelectExpressionPathWithMethodName(parameter, path, selectCollectionHandling, nullChecking, "ToList");
else if (selectType == SelectTypes.First)
return CreateSelectExpressionPathWithMethodName(parameter, path, selectCollectionHandling, nullChecking, "First");
else if (selectType == SelectTypes.FirstOrDefault)
return CreateSelectExpressionPathWithMethodName(parameter, path, selectCollectionHandling, nullChecking, "FirstOrDefault");
else if (selectType == SelectTypes.Last)
return CreateSelectExpressionPathWithMethodName(parameter, path, selectCollectionHandling, nullChecking, "Last");
else if (selectType == SelectTypes.LastOrDefault)
return CreateSelectExpressionPathWithMethodName(parameter, path, selectCollectionHandling, nullChecking, "LastOrDefault");
var body = Expression.Call(typeof(Enumerable), "ToList", new[] { notGroupedType }, expr) as Expression;
return body;
}
throw new NotSupportedException($"unkown select type {selectType}");
}
private static Expression CreateSelectExpressionPathWithMethodName(ParameterExpression parameter, string path, SelectCollectionHandling selectCollectionHandling, bool nullChecking, string methodName)
{
var parser = new ExpressionParser(parameter, path);
var resolver = new PathExpressionResolver(parser);
resolver.NullChecking = nullChecking;
resolver.CollectionHandling = selectCollectionHandling;
resolver.Resolve();
var expr = (resolver.Result as LambdaExpression).Body;
var notGroupedType = expr.Type.GenericTypeArguments.FirstOrDefault();
if (notGroupedType == null)
throw new Exception($"Path must be a Enumerable<T> but its a {expr.Type}");
var body = Expression.Call(typeof(Enumerable), methodName, new[] { notGroupedType }, expr) as Expression;
return body;
}
private static bool IsGrouping(IQueryable query)
{
// TODO needs to be alot better than this, but it will do for now.