regular selectables.

This commit is contained in:
David Lebée
2018-03-21 20:00:46 -05:00
parent 3f5433ba53
commit 57fbee664e
5 changed files with 106 additions and 2 deletions
+3 -1
View File
@@ -48,7 +48,9 @@ namespace PoweredSoft.DynamicLinq
LongCount,
Sum,
Average,
ToList
ToList,
PathToList,
Path
}
internal static class Constants
@@ -51,6 +51,23 @@ namespace PoweredSoft.DynamicLinq.Fluent
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);
@@ -116,6 +133,23 @@ namespace PoweredSoft.DynamicLinq.Fluent
return this;
}
public SelectBuilder PathToList(string path, string propertyName = null)
{
if (propertyName == null)
propertyName = path.Split('.').LastOrDefault();
throwIfUsedOrEmpty(propertyName);
Parts.Add(new SelectPart
{
Path = path,
PropertyName = propertyName,
SelectType = SelectTypes.PathToList
});
return this;
}
public virtual IQueryable Build()
{
if (Empty)
@@ -196,11 +196,31 @@ namespace PoweredSoft.DynamicLinq.Helpers
private static Expression CreateSelectExpression(IQueryable query, ParameterExpression parameter, SelectTypes selectType, string path)
{
if (!IsGrouping(query))
throw new NotSupportedException("Select without grouping is not supported yet.");
return CreateSelectExpressionRegular(query, parameter, selectType, path);
return CreateSelectExpressionForGrouping(query, parameter, selectType, path);
}
private static Expression CreateSelectExpressionRegular(IQueryable query, ParameterExpression parameter, SelectTypes selectType, string path)
{
if (selectType == SelectTypes.Path)
{
return ResolvePathForExpression(parameter, path);
}
else if (selectType == SelectTypes.PathToList)
{
var expr = ResolvePathForExpression(parameter, path);
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), "ToList", new[] { notGroupedType }, expr);
return body;
}
throw new NotSupportedException($"unkown select type {selectType}");
}
private static bool IsGrouping(IQueryable query)
{
// TODO needs to be alot better than this, but it will do for now.