adding lots more select types support.

This commit is contained in:
David Lebee
2018-10-25 20:02:17 -05:00
parent 7269071a3a
commit dcaf114a09
4 changed files with 92 additions and 119 deletions
+7 -1
View File
@@ -50,7 +50,13 @@ namespace PoweredSoft.DynamicLinq
Average,
ToList,
PathToList,
Path
Path,
Min,
Max,
LastOrDefault,
FirstOrDefault,
Last,
First
}
public enum SelectCollectionHandling
@@ -42,25 +42,11 @@ namespace PoweredSoft.DynamicLinq.Fluent
throw new Exception($"{propertyName} is already used");
}
public SelectBuilder Key(string propertyName, string path = null)
public SelectBuilder Aggregate(string path, SelectTypes type, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
{
if (propertyName == null)
propertyName = path.Split('.').LastOrDefault();
if (propertyName == null && path == null)
throw new Exception("if property name is not specified, a path must be supplied.");
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
Path = path == null ? "Key" : $"Key.{path}",
PropertyName = propertyName,
SelectType = SelectTypes.Key
});
return this;
}
public SelectBuilder Aggregate(string path, SelectTypes type, string propertyName = null)
{
if (propertyName == null)
propertyName = path.Split('.').LastOrDefault();
@@ -70,112 +56,31 @@ namespace PoweredSoft.DynamicLinq.Fluent
{
Path = path,
PropertyName = propertyName,
SelectType = type
});
return this;
}
public SelectBuilder Path(string path, string propertyName = null)
{
if (propertyName == null)
propertyName = path.Split('.').LastOrDefault();
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
Path = path,
PropertyName = propertyName,
SelectType = SelectTypes.Path
});
return this;
}
public SelectBuilder Count(string propertyName)
{
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
PropertyName = propertyName,
SelectType = SelectTypes.Count
});
return this;
}
public SelectBuilder LongCount(string propertyName)
{
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
PropertyName = propertyName,
SelectType = SelectTypes.LongCount
});
return this;
}
public SelectBuilder Sum(string path, string propertyName = null)
{
if (propertyName == null)
propertyName = path.Split('.').LastOrDefault();
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
Path = path,
PropertyName = propertyName,
SelectType = SelectTypes.Sum
});
return this;
}
public SelectBuilder Average(string path, string propertyName = null)
{
if (propertyName == null)
propertyName = path.Split('.').LastOrDefault();
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
Path = path,
PropertyName = propertyName,
SelectType = SelectTypes.Average
});
return this;
}
public SelectBuilder ToList(string propertyName)
{
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
PropertyName = propertyName,
SelectType = SelectTypes.ToList
});
return this;
}
public SelectBuilder PathToList(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
{
if (propertyName == null)
propertyName = path.Split('.').LastOrDefault();
ThrowIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
Path = path,
PropertyName = propertyName,
SelectType = SelectTypes.PathToList,
SelectType = type,
SelectCollectionHandling = selectCollectionHandling
});
return this;
}
public SelectBuilder Key(string propertyName, string path = null) => Aggregate(path == null ? "Key" : $"Key.{path}", SelectTypes.Key, propertyName);
public SelectBuilder Path(string path, string propertyName = null) => Aggregate(path, SelectTypes.Path, propertyName);
public SelectBuilder Count(string propertyName) => Aggregate(null, SelectTypes.Count, propertyName);
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 ToList(string propertyName) => Aggregate(null, SelectTypes.ToList, propertyName);
public SelectBuilder PathToList(string path, string propertyName = null, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs)
=> Aggregate(path, SelectTypes.PathToList, 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 virtual IQueryable Build()
{
if (Empty)
@@ -175,12 +175,54 @@ namespace PoweredSoft.DynamicLinq.Helpers
var body = Expression.Call(typeof(Enumerable), "Sum", new[] { notGroupedType }, parameter, innerMemberLambda);
return body;
}
else if (selectType == SelectTypes.Min)
{
var notGroupedType = parameter.Type.GenericTypeArguments[1];
var innerParameter = Expression.Parameter(notGroupedType);
var innerMemberExpression = ResolvePathForExpression(innerParameter, path);
var innerMemberLambda = Expression.Lambda(innerMemberExpression, innerParameter);
var body = Expression.Call(typeof(Enumerable), "Min", new[] { notGroupedType }, parameter, innerMemberLambda);
return body;
}
else if (selectType == SelectTypes.Max)
{
var notGroupedType = parameter.Type.GenericTypeArguments[1];
var innerParameter = Expression.Parameter(notGroupedType);
var innerMemberExpression = ResolvePathForExpression(innerParameter, path);
var innerMemberLambda = Expression.Lambda(innerMemberExpression, innerParameter);
var body = Expression.Call(typeof(Enumerable), "Max", new[] { notGroupedType }, parameter, innerMemberLambda);
return body;
}
else if (selectType == SelectTypes.ToList)
{
var notGroupedType = parameter.Type.GenericTypeArguments[1];
var body = Expression.Call(typeof(Enumerable), "ToList", new[] { notGroupedType }, parameter);
return body;
}
else if (selectType == SelectTypes.First)
{
var notGroupedType = parameter.Type.GenericTypeArguments[1];
var body = Expression.Call(typeof(Enumerable), "First", new[] { notGroupedType }, parameter);
return body;
}
else if (selectType == SelectTypes.Last)
{
var notGroupedType = parameter.Type.GenericTypeArguments[1];
var body = Expression.Call(typeof(Enumerable), "Last", new[] { notGroupedType }, parameter);
return body;
}
else if (selectType == SelectTypes.FirstOrDefault)
{
var notGroupedType = parameter.Type.GenericTypeArguments[1];
var body = Expression.Call(typeof(Enumerable), "FirstOrDefault", new[] { notGroupedType }, parameter);
return body;
}
else if (selectType == SelectTypes.LastOrDefault)
{
var notGroupedType = parameter.Type.GenericTypeArguments[1];
var body = Expression.Call(typeof(Enumerable), "LastOrDefault", new[] { notGroupedType }, parameter);
return body;
}
throw new NotSupportedException($"unkown select type {selectType}");
}