cleaned up a bit.
This commit is contained in:
parent
3ca718da52
commit
3b9494d510
@ -137,128 +137,5 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private Expression JoinGroups(IEnumerable<ExpressionParserPieceGroup> groups, Expression result = null)
|
|
||||||
{
|
|
||||||
var lastGroup = groups.Last();
|
|
||||||
var lastGroupExpression = lastGroup.CompileGroup(NullHandling);
|
|
||||||
var lastGroupExpressionLambda = Expression.Lambda(lastGroupExpression, lastGroup.Parameter);
|
|
||||||
|
|
||||||
if (lastGroup.Parent == null)
|
|
||||||
return lastGroupExpressionLambda;
|
|
||||||
|
|
||||||
var parent = lastGroup.Parent;
|
|
||||||
var parentExpression = parent.CompileGroup(NullHandling);
|
|
||||||
var selectType = parent.GroupEnumerableType();
|
|
||||||
|
|
||||||
var select = Expression.Call(typeof(Enumerable), "Select",
|
|
||||||
new Type[] { selectType, lastGroupExpression.Type },
|
|
||||||
parentExpression, lastGroupExpressionLambda);
|
|
||||||
return select;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Expression RecursiveSelect(List<ExpressionParserPiece> pieces)
|
|
||||||
{
|
|
||||||
// get the last.
|
|
||||||
var piece = pieces.Last();
|
|
||||||
|
|
||||||
var firstEnumerableParent = ExpressionParser.GetFirstEnumerableParent(piece);
|
|
||||||
var chainTillEnumerable = pieces.SkipWhile(t => t != firstEnumerableParent).Skip(1).ToList();
|
|
||||||
if (chainTillEnumerable.Count == 0)
|
|
||||||
{
|
|
||||||
if (piece.Parent == null)
|
|
||||||
{
|
|
||||||
// no parent.
|
|
||||||
var mostLeftExpression = Expression.PropertyOrField(Parser.Parameter, piece.Name);
|
|
||||||
return mostLeftExpression;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// we have a parent its probably a enumerable.
|
|
||||||
var collectionParentParameter = Expression.Parameter(piece.Parent.EnumerableType);
|
|
||||||
var memberOf = Expression.PropertyOrField(collectionParentParameter, piece.Name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// we have a simple chain to resolve.
|
|
||||||
var chainTillEnumerableParameter = ResolveParameter(chainTillEnumerable);
|
|
||||||
var chainTillEnumerableExpression = BuildSimpleChainExpression(chainTillEnumerable, chainTillEnumerableParameter);
|
|
||||||
var chainTillEnumerableLambda = Expression.Lambda(chainTillEnumerableExpression, chainTillEnumerableParameter);
|
|
||||||
|
|
||||||
// get parent to glue with.
|
|
||||||
var nextList = pieces.Take(pieces.IndexOf(firstEnumerableParent)+1).ToList();
|
|
||||||
var parent = RecursiveSelect(nextList);
|
|
||||||
|
|
||||||
// glue.
|
|
||||||
var gluedResult = Expression.Call(typeof(Enumerable),
|
|
||||||
"Select",
|
|
||||||
new Type[] { firstEnumerableParent.EnumerableType, chainTillEnumerableExpression.Type },
|
|
||||||
parent, chainTillEnumerableLambda);
|
|
||||||
|
|
||||||
return gluedResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw null;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExpressionParserPiece GetFirstEnumerableParent(ExpressionParserPiece piece)
|
|
||||||
{
|
|
||||||
if (piece.Parent == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
if (piece.Parent.IsGenericEnumerable)
|
|
||||||
return piece.Parent;
|
|
||||||
|
|
||||||
return GetFirstEnumerableParent(piece.Parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Expression NullCheckTernary(Expression left, Expression right, ParameterExpression parameter)
|
|
||||||
{
|
|
||||||
var lastPiece = Parser.Pieces.Last();
|
|
||||||
var lastPieceType = !lastPiece.IsGenericEnumerable ? lastPiece.Type : lastPiece.EnumerableType;
|
|
||||||
|
|
||||||
var escapeType = typeof(List<>).MakeGenericType(lastPieceType);
|
|
||||||
var typeMatch = typeof(IEnumerable<>).MakeGenericType(lastPieceType);
|
|
||||||
var ifTrueExpression = Expression.New(escapeType);
|
|
||||||
var testExpression = Expression.Equal(left, Expression.Constant(null));
|
|
||||||
|
|
||||||
var condition = Expression.Condition(testExpression, ifTrueExpression, right, typeMatch);
|
|
||||||
var lambda = Expression.Lambda(condition, parameter);
|
|
||||||
return lambda;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Expression BuildNullCheckConditionExpressionForWhere(List<ExpressionParserPiece> currentSimpleChain, ParameterExpression parameter)
|
|
||||||
{
|
|
||||||
var path = string.Join(".", currentSimpleChain.Select(t => t.Name).Take(currentSimpleChain.Count - 1));
|
|
||||||
var where = QueryableHelpers.CreateConditionExpression(parameter.Type, path, ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
|
||||||
nullChecking: true, parameter: parameter);
|
|
||||||
return where;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ParameterExpression ResolveParameter(List<ExpressionParserPiece> currentSimpleChain)
|
|
||||||
{
|
|
||||||
var first = currentSimpleChain.First();
|
|
||||||
if (first.Parent == null)
|
|
||||||
return Parser.Parameter;
|
|
||||||
|
|
||||||
if (first.Parent.IsGenericEnumerable)
|
|
||||||
return Expression.Parameter(first.Parent.EnumerableType);
|
|
||||||
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Expression BuildSimpleChainExpression(List<ExpressionParserPiece> chain, ParameterExpression parameter)
|
|
||||||
{
|
|
||||||
var ret = parameter as Expression;
|
|
||||||
chain.ForEach(p =>
|
|
||||||
{
|
|
||||||
var me = Expression.PropertyOrField(ret, p.Name);
|
|
||||||
ret = me;
|
|
||||||
});
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user