using PoweredSoft.DynamicLinq.Dal.Pocos; using PoweredSoft.DynamicLinq.Helpers; using PoweredSoft.DynamicLinq.Parser; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace PoweredSoft.DynamicLinq.ConsoleApp { public class BetterProto2 { /* * (parent, innerExpression, innerExpressionLambda) => { var listGenericArgumentType = parent.Type.GetGenericArguments().First(); Expression ret = null; if (selectCollectionHandling == SelectCollectionHandling.LeaveAsIs || !QueryableHelpers.IsGenericEnumerable(innerExpression)) ret = Expression.Call(typeof(Enumerable), "Select", new Type[] { listGenericArgumentType, innerExpression.Type }, parent, innerExpressionLambda); else ret = Expression.Call(typeof(Enumerable), "SelectMany", new Type[] { listGenericArgumentType, innerExpression.Type.GenericTypeArguments.First() }, parent, innerExpressionLambda); return ret; } */ public static void Run() { //Case1(); Case2(); } public static void Case1() { // the expression parser. var ep = new ExpressionParser(typeof(Author), "Posts.Comments.Id"); // the builder. var per = new PathExpressionResolver(ep); per.Resolve(); // the result expression. var result = per.Result; } public static void Case2() { // the expression parser. var ep = new ExpressionParser(typeof(Author), "Posts.Author.Posts.Author.Website.Url"); new List().AsQueryable().Select(t => new { A = t.Posts.Select(t2 => t2.Author.Posts.Select(t3 => t3.Author.Website.Url)) }); new List().AsQueryable().Select(t => new { FirstNames = t.Author == null ? new List() : (t.Author.Posts == null ? new List() : t.Author.Posts.Where(t2 => t2.Author != null).Select(t2 => t2.Author.FirstName)), PostsAuthors = t.Author == null ? new List() : (t.Author.Posts == null ? new List() : t.Author.Posts.Where(t2 => t2.Author != null).Select(t2 => t2.Author)), Comments = t.Comments == null ? new List() : t.Comments, CommentLikes = (t.Comments == null ? new List() : t.Comments.Where(t2 => t2.CommentLikes != null).SelectMany(t2 => t2.CommentLikes)), CommentLikeIds = (t.Comments == null ? new List() : t.Comments.Where(t2 => t2.CommentLikes != null).SelectMany(t2 => t2.CommentLikes.Select(t3 => t3.Id))), CommentsLikes = (t.Comments == null ? new List>() : t.Comments.Where(t2 => t2.CommentLikes != null).Select(t2 => t2.CommentLikes)) }); // the builder. var per = new PathExpressionResolver(ep); per.NullHandling = SelectNullHandling.Handle; per.Resolve(); // the result expression. var result = per.Result; } } public class PathExpressionResolver { public SelectNullHandling NullHandling { get; set; } = SelectNullHandling.LeaveAsIs; public SelectCollectionHandling CollectionHandling { get; set; } = SelectCollectionHandling.LeaveAsIs; public ExpressionParser Parser { get; protected set; } public Expression Result { get; protected set; } public PathExpressionResolver(ExpressionParser parser) { Parser = parser; } public void Resolve() { Result = null; // parse the expression. Parser.Parse(); // group the piece by common parameters var groups = Parser.GroupBySharedParameters(); Expression ce = null; groups.ReversedForEach(group => { if (ce == null) { var ge = group.CompileGroup(NullHandling); var gl = Expression.Lambda(ge, group.Parameter); if (group.Parent == null) { ce = gl; return; } var parent = group.Parent; var parentExpression = parent.CompileGroup(NullHandling); var selectType = parent.GroupEnumerableType(); var select = Expression.Call(typeof(Enumerable), "Select", new Type[] { selectType, ge.Type }, parentExpression, gl); ce = select; } else { if (group.Parent == null) { ce = Expression.Lambda(ce, group.Parameter); return; } var parent = group.Parent; var parentExpression = parent.CompileGroup(NullHandling); var selectType = parent.GroupEnumerableType(); var currentExpressionLambda = Expression.Lambda(ce, group.Parameter); ce = Expression.Call(typeof(Enumerable), "Select", new Type[] { selectType, ce.Type }, parentExpression, currentExpressionLambda); } }); } private Expression JoinGroups(IEnumerable 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 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 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 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 chain, ParameterExpression parameter) { var ret = parameter as Expression; chain.ForEach(p => { var me = Expression.PropertyOrField(ret, p.Name); ret = me; }); return ret; } } }