omg finally.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using PoweredSoft.DynamicLinq.DynamicType;
|
||||
using PoweredSoft.DynamicLinq.Parser;
|
||||
using PoweredSoft.DynamicLinq.Resolver;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
@@ -201,7 +203,8 @@ namespace PoweredSoft.DynamicLinq.Helpers
|
||||
return CreateSelectExpressionForGrouping(query, parameter, selectType, path, selectCollectionHandling, nullChecking);
|
||||
}
|
||||
|
||||
private static Expression CreateSelectExpressionRegular(IQueryable query, ParameterExpression parameter, SelectTypes selectType, string path, SelectCollectionHandling selectCollectionHandling, bool nullChecking)
|
||||
private static Expression CreateSelectExpressionRegular(IQueryable query, ParameterExpression parameter, SelectTypes selectType, string path
|
||||
, SelectCollectionHandling selectCollectionHandling, bool nullChecking)
|
||||
{
|
||||
if (selectType == SelectTypes.Path)
|
||||
{
|
||||
@@ -209,7 +212,12 @@ namespace PoweredSoft.DynamicLinq.Helpers
|
||||
}
|
||||
else if (selectType == SelectTypes.PathToList)
|
||||
{
|
||||
var expr = ResolvePathForExpression(parameter, path);
|
||||
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}");
|
||||
|
||||
@@ -83,6 +83,10 @@ namespace PoweredSoft.DynamicLinq.Parser
|
||||
group = CreateAndAddGroup(groups, Expression.Parameter(piece.EnumerableType), group);
|
||||
});
|
||||
|
||||
// if the last piece is empty.
|
||||
if (group.Pieces.Count == 0)
|
||||
groups.Remove(group);
|
||||
|
||||
return groups;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
using PoweredSoft.DynamicLinq.Helpers;
|
||||
using PoweredSoft.DynamicLinq.Parser;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Resolver
|
||||
{
|
||||
public class PathExpressionResolver
|
||||
{
|
||||
public bool NullChecking { get; set; } = false;
|
||||
public SelectCollectionHandling CollectionHandling { get; set; } = SelectCollectionHandling.LeaveAsIs;
|
||||
public ExpressionParser Parser { get; protected set; }
|
||||
|
||||
public Expression Result { get; protected set; }
|
||||
public Type NullType { get; set; }
|
||||
|
||||
public PathExpressionResolver(ExpressionParser parser)
|
||||
{
|
||||
Parser = parser;
|
||||
}
|
||||
|
||||
protected Expression CompileGroup(ExpressionParserPieceGroup group, bool nullChecking)
|
||||
{
|
||||
var expr = group.Parameter as Expression;
|
||||
group.Pieces.ForEach(piece =>
|
||||
{
|
||||
expr = Expression.PropertyOrField(expr, piece.Name);
|
||||
});
|
||||
return expr;
|
||||
}
|
||||
|
||||
public void Resolve()
|
||||
{
|
||||
Result = null;
|
||||
|
||||
// parse the expression.
|
||||
Parser.Parse();
|
||||
|
||||
// group the piece by common parameters
|
||||
var groups = Parser.GroupBySharedParameters();
|
||||
|
||||
Expression currentExpression = null;
|
||||
foreach (var group in groups.Reversed())
|
||||
{
|
||||
var isLastGroup = groups.IndexOf(group) == groups.Count - 1;
|
||||
|
||||
if (currentExpression == null)
|
||||
{
|
||||
var groupExpression = CompileGroup(group, NullChecking);
|
||||
var groupExpressionLambda = Expression.Lambda(groupExpression, group.Parameter);
|
||||
|
||||
if (group.Parent == null)
|
||||
{
|
||||
currentExpression = groupExpressionLambda;
|
||||
continue;
|
||||
}
|
||||
|
||||
var parent = group.Parent;
|
||||
var parentExpression = CompileGroup(parent, NullChecking);
|
||||
|
||||
// check null with where.
|
||||
if (NullChecking != false)
|
||||
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup);
|
||||
|
||||
// the select expression.
|
||||
var selectType = parent.GroupEnumerableType();
|
||||
var selectExpression = Expression.Call(typeof(Enumerable), "Select",
|
||||
new Type[] { selectType, groupExpression.Type },
|
||||
parentExpression, groupExpressionLambda);
|
||||
currentExpression = selectExpression;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (group.Parent == null)
|
||||
{
|
||||
if (NullChecking != false)
|
||||
currentExpression = CheckNullOnFirstGroup(group, currentExpression);
|
||||
|
||||
currentExpression = Expression.Lambda(currentExpression, group.Parameter);
|
||||
continue;
|
||||
}
|
||||
|
||||
var parent = group.Parent;
|
||||
var parentExpression = CompileGroup(parent, NullChecking);
|
||||
var selectType = parent.GroupEnumerableType();
|
||||
|
||||
|
||||
if (NullChecking != false)
|
||||
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup);
|
||||
|
||||
var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter);
|
||||
currentExpression = Expression.Call(typeof(Enumerable), "Select",
|
||||
new Type[] { selectType, currentExpression.Type },
|
||||
parentExpression, currentExpressionLambda);
|
||||
}
|
||||
}
|
||||
|
||||
Result = currentExpression;
|
||||
}
|
||||
|
||||
private Expression CheckNullOnFirstGroup(ExpressionParserPieceGroup group, Expression currentExpression)
|
||||
{
|
||||
var path = string.Join(".", group.Pieces.Select(t => t.Name));
|
||||
var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path,
|
||||
ConditionOperators.Equal, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
||||
parameter: group.Parameter, nullChecking: true);
|
||||
|
||||
var whereBodyExpression = (whereExpression as LambdaExpression).Body;
|
||||
|
||||
var nullType = currentExpression.Type;
|
||||
Expression ifTrueExpression = null;
|
||||
if (QueryableHelpers.IsGenericEnumerable(nullType))
|
||||
{
|
||||
var listType = typeof(List<>).MakeGenericType(nullType.GenericTypeArguments.First());
|
||||
ifTrueExpression = Expression.New(listType);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ifTrueExpression = Expression.Default(NullType);
|
||||
}
|
||||
|
||||
return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type);
|
||||
}
|
||||
|
||||
private static Expression CheckNullOnEnumerableParent(ExpressionParserPieceGroup group, ExpressionParserPieceGroup parent, Expression parentExpression, bool isLastGroup)
|
||||
{
|
||||
string path = null;
|
||||
if (isLastGroup)
|
||||
path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(t => t.Name));
|
||||
else
|
||||
path = string.Join(".", group.Pieces.Select(t => t.Name));
|
||||
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path,
|
||||
ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
||||
parameter: group.Parameter, nullChecking: true);
|
||||
|
||||
//public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
|
||||
parentExpression = Expression.Call(typeof(Enumerable), "Where",
|
||||
new Type[] { parent.GroupEnumerableType() },
|
||||
parentExpression, whereExpression);
|
||||
}
|
||||
|
||||
return parentExpression;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user