i imagine now i should be able to create ternary conditions since im going from inner to outside (Condition expressions).

This commit is contained in:
David Lebee 2018-03-27 19:22:27 -05:00
parent 2f4c406503
commit 1a4bde89df
2 changed files with 12 additions and 30 deletions

View File

@ -14,42 +14,18 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
var q = new List<Author>().AsQueryable(); var q = new List<Author>().AsQueryable();
q.Select(t => new q.Select(t => new
{ {
Ids = t.Posts.SelectMany(t2 => t2.Comments.Select(t3 => t3.CommentLikes)) Ids = t.Posts.SelectMany(t2 => t2.Comments.Select(t3 => t3.CommentLikes))
}); });
var expressionParser = new ExpressionParser(typeof(Author), "Posts.Comments.Id"); var expressionParser = new ExpressionParser(typeof(Author), "Posts.Comments.CommentLikes");
expressionParser.Parse(); expressionParser.Parse();
var finalExpression = CreateSelectExpressionFromParsed(expressionParser, SelectCollectionHandling.Flatten, SelectNullHandling.LeaveAsIs); var finalExpression = CreateSelectExpressionFromParsed(expressionParser, SelectCollectionHandling.Flatten, SelectNullHandling.LeaveAsIs);
} }
/*
* var type = typeof(Dal.Pocos.Author);
var param = Expression.Parameter(type);
var parts = ExpressionPathPart.Split(param, "Posts.Comments.Id");
*
// add the last part.
var parent = parts.First();
var last = parts.Last();
var toListType = last.GetToListType();
parts.Add(new ExpressionPathPart
{
ParentExpression = parent.PartExpression,
PartExpression = Expression.Call(typeof(Enumerable), "ToList", new[] { toListType }, parent.PartExpression)
});*/
//var result = CreateSelectExpressionFromParts(parts, SelectCollectionHandling.Flatten, SelectNullHandling.New);
//}
public static Expression CreateSelectExpressionFromParsed(ExpressionParser expressionParser, public static Expression CreateSelectExpressionFromParsed(ExpressionParser expressionParser,
SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs, SelectCollectionHandling selectCollectionHandling = SelectCollectionHandling.LeaveAsIs,
SelectNullHandling nullChecking = SelectNullHandling.LeaveAsIs) SelectNullHandling nullChecking = SelectNullHandling.LeaveAsIs)
@ -78,9 +54,9 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp
if (true == t.Parent?.LastPiece().IsGenericEnumerable()) if (true == t.Parent?.LastPiece().IsGenericEnumerable())
{ {
if (lastSelectExpression == null) if (lastSelectExpression == null)
lastSelectExpression = CreateSelectExpression(t, selectCollectionHandling); lastSelectExpression = RegroupSelectExpressions(t, selectCollectionHandling);
else else
lastSelectExpression = CreateSelectExpression(t, lastSelectExpression, selectCollectionHandling); lastSelectExpression = RegroupSelectExpressions(t, lastSelectExpression, selectCollectionHandling);
} }
} }
@ -88,7 +64,7 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp
return ret; return ret;
} }
public static MethodCallExpression CreateSelectExpression(ExpressionParameterGroup group, SelectCollectionHandling selectCollectionHandling) public static MethodCallExpression RegroupSelectExpressions(ExpressionParameterGroup group, SelectCollectionHandling selectCollectionHandling)
{ {
MethodCallExpression ret = null; MethodCallExpression ret = null;
var lambda = group.CreateLambda(); var lambda = group.CreateLambda();
@ -105,7 +81,7 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp
return ret; return ret;
} }
public static MethodCallExpression CreateSelectExpression(ExpressionParameterGroup group, MethodCallExpression innerSelect, SelectCollectionHandling selectCollectionHandling) public static MethodCallExpression RegroupSelectExpressions(ExpressionParameterGroup group, MethodCallExpression innerSelect, SelectCollectionHandling selectCollectionHandling)
{ {
var parent = group.Parent; var parent = group.Parent;
var parentLastPiece = parent.LastPiece(); var parentLastPiece = parent.LastPiece();

View File

@ -86,13 +86,19 @@ namespace PoweredSoft.DynamicLinq.Helpers
var currentGroup = CreateAndAddParameterGroup(Parameter); var currentGroup = CreateAndAddParameterGroup(Parameter);
ExpressionPiece parentPiece = null; ExpressionPiece parentPiece = null;
int step = 0; int indexOfPiece = -1;
pieces.ForEach(piece => pieces.ForEach(piece =>
{ {
++indexOfPiece;
bool isLast = indexOfPiece == pieces.Count - 1;
var expressionPiece = new ExpressionPiece(currentGroup, parentPiece); var expressionPiece = new ExpressionPiece(currentGroup, parentPiece);
expressionPiece.Resolve(piece); expressionPiece.Resolve(piece);
currentGroup.AddSubPart(expressionPiece); currentGroup.AddSubPart(expressionPiece);
// rest is only if its not the last piece.
if (isLast) return;
if (expressionPiece.IsGenericEnumerable()) if (expressionPiece.IsGenericEnumerable())
{ {
var param = Expression.Parameter(expressionPiece.GetGenericEnumerableType()); var param = Expression.Parameter(expressionPiece.GetGenericEnumerableType());