2018-04-02 12:47:30 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Dal.Pocos;
|
|
|
|
|
using PoweredSoft.DynamicLinq.Helpers;
|
2018-04-05 18:54:28 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Parser;
|
2018-04-02 12:47:30 -04:00
|
|
|
|
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()
|
2018-04-02 15:09:57 -04:00
|
|
|
|
{
|
|
|
|
|
//Case1();
|
|
|
|
|
Case2();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Case1()
|
2018-04-02 12:47:30 -04:00
|
|
|
|
{
|
2018-04-02 12:57:46 -04:00
|
|
|
|
// the expression parser.
|
2018-04-02 12:47:30 -04:00
|
|
|
|
var ep = new ExpressionParser(typeof(Author), "Posts.Comments.Id");
|
2018-04-02 12:57:46 -04:00
|
|
|
|
|
|
|
|
|
// the builder.
|
|
|
|
|
var per = new PathExpressionResolver(ep);
|
|
|
|
|
per.Resolve();
|
|
|
|
|
|
|
|
|
|
// the result expression.
|
|
|
|
|
var result = per.Result;
|
2018-04-02 12:47:30 -04:00
|
|
|
|
}
|
2018-04-02 15:09:57 -04:00
|
|
|
|
|
|
|
|
|
public static void Case2()
|
|
|
|
|
{
|
|
|
|
|
// the expression parser.
|
2018-04-05 18:54:28 -04:00
|
|
|
|
var ep = new ExpressionParser(typeof(Author), "Posts.Author.Posts.Author.Website.Url");
|
|
|
|
|
|
|
|
|
|
new List<Author>().AsQueryable().Select(t => new
|
|
|
|
|
{
|
2018-04-10 20:08:31 -04:00
|
|
|
|
A = t.Posts.Select(t2 => t2.Author.Posts.Select(t3 => t3.Author.Website.Url)) ,
|
|
|
|
|
B = t.Posts.Where(t2 => t2.Author != null).Select(t2 => t2.Author.Posts.Where(t3 => t3.Author != null && t3.Author.Website != null).Select(t3 => t3.Author.Website.Url))
|
2018-04-05 18:54:28 -04:00
|
|
|
|
});
|
2018-04-02 15:37:49 -04:00
|
|
|
|
|
2018-04-02 16:01:35 -04:00
|
|
|
|
new List<Post>().AsQueryable().Select(t => new
|
2018-04-02 15:37:49 -04:00
|
|
|
|
{
|
2018-04-02 16:01:35 -04:00
|
|
|
|
FirstNames = t.Author == null ? new List<string>() : (t.Author.Posts == null ? new List<string>() : t.Author.Posts.Where(t2 => t2.Author != null).Select(t2 => t2.Author.FirstName)),
|
2018-04-02 16:04:18 -04:00
|
|
|
|
PostsAuthors = t.Author == null ? new List<Author>() : (t.Author.Posts == null ? new List<Author>() : t.Author.Posts.Where(t2 => t2.Author != null).Select(t2 => t2.Author)),
|
2018-04-02 16:01:35 -04:00
|
|
|
|
Comments = t.Comments == null ? new List<Comment>() : t.Comments,
|
|
|
|
|
CommentLikes = (t.Comments == null ? new List<CommentLike>() : t.Comments.Where(t2 => t2.CommentLikes != null).SelectMany(t2 => t2.CommentLikes)),
|
|
|
|
|
CommentLikeIds = (t.Comments == null ? new List<long>() : t.Comments.Where(t2 => t2.CommentLikes != null).SelectMany(t2 => t2.CommentLikes.Select(t3 => t3.Id))),
|
|
|
|
|
CommentsLikes = (t.Comments == null ? new List<List<CommentLike>>() : t.Comments.Where(t2 => t2.CommentLikes != null).Select(t2 => t2.CommentLikes))
|
|
|
|
|
});
|
2018-04-02 15:09:57 -04:00
|
|
|
|
|
|
|
|
|
// the builder.
|
|
|
|
|
var per = new PathExpressionResolver(ep);
|
2018-04-05 20:28:07 -04:00
|
|
|
|
per.NullHandling = SelectNullHandling.Handle;
|
2018-04-02 15:09:57 -04:00
|
|
|
|
per.Resolve();
|
|
|
|
|
|
|
|
|
|
// the result expression.
|
|
|
|
|
var result = per.Result;
|
|
|
|
|
}
|
2018-04-02 12:47:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 18:54:28 -04:00
|
|
|
|
|
2018-04-02 12:47:30 -04:00
|
|
|
|
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public class PathExpressionResolver
|
|
|
|
|
{
|
2018-04-05 20:28:07 -04:00
|
|
|
|
public SelectNullHandling NullHandling { get; set; } = SelectNullHandling.LeaveAsIs;
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public SelectCollectionHandling CollectionHandling { get; set; } = SelectCollectionHandling.LeaveAsIs;
|
|
|
|
|
public ExpressionParser Parser { get; protected set; }
|
2018-04-05 18:54:28 -04:00
|
|
|
|
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public Expression Result { get; protected set; }
|
2018-04-10 20:00:40 -04:00
|
|
|
|
public Type NullType { get; set; }
|
2018-04-02 12:57:46 -04:00
|
|
|
|
|
|
|
|
|
public PathExpressionResolver(ExpressionParser parser)
|
|
|
|
|
{
|
|
|
|
|
Parser = parser;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-10 20:00:40 -04:00
|
|
|
|
protected Expression CompileGroup(ExpressionParserPieceGroup group, SelectNullHandling NullHandling)
|
|
|
|
|
{
|
|
|
|
|
var expr = group.Parameter as Expression;
|
|
|
|
|
group.Pieces.ForEach(piece =>
|
|
|
|
|
{
|
|
|
|
|
expr = Expression.PropertyOrField(expr, piece.Name);
|
|
|
|
|
});
|
|
|
|
|
return expr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public void Resolve()
|
|
|
|
|
{
|
2018-04-02 15:09:57 -04:00
|
|
|
|
Result = null;
|
|
|
|
|
|
|
|
|
|
// parse the expression.
|
|
|
|
|
Parser.Parse();
|
2018-04-02 12:57:46 -04:00
|
|
|
|
|
2018-04-05 18:54:28 -04:00
|
|
|
|
// group the piece by common parameters
|
|
|
|
|
var groups = Parser.GroupBySharedParameters();
|
2018-04-04 22:57:40 -04:00
|
|
|
|
|
2018-04-10 20:00:40 -04:00
|
|
|
|
NullType = groups.ResolveNullHandlingType();
|
2018-04-05 20:40:33 -04:00
|
|
|
|
|
2018-04-05 20:29:50 -04:00
|
|
|
|
Expression currentExpression = null;
|
2018-04-05 20:28:07 -04:00
|
|
|
|
groups.ReversedForEach(group =>
|
|
|
|
|
{
|
2018-04-05 20:29:50 -04:00
|
|
|
|
if (currentExpression == null)
|
2018-04-05 20:28:07 -04:00
|
|
|
|
{
|
2018-04-10 20:00:40 -04:00
|
|
|
|
var groupExpression = CompileGroup(group, NullHandling);
|
2018-04-05 20:29:50 -04:00
|
|
|
|
var groupExpressionLambda = Expression.Lambda(groupExpression, group.Parameter);
|
2018-04-05 20:28:07 -04:00
|
|
|
|
|
|
|
|
|
if (group.Parent == null)
|
|
|
|
|
{
|
2018-04-05 20:29:50 -04:00
|
|
|
|
currentExpression = groupExpressionLambda;
|
2018-04-05 20:28:07 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parent = group.Parent;
|
2018-04-10 20:00:40 -04:00
|
|
|
|
var parentExpression = CompileGroup(parent, NullHandling);
|
|
|
|
|
|
|
|
|
|
// check null with where.
|
|
|
|
|
if (NullHandling != SelectNullHandling.LeaveAsIs)
|
|
|
|
|
{
|
|
|
|
|
if (group.Pieces.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
var path = string.Join(".", group.Pieces.Take(group.Pieces.Count-1).Select(T => T.Name));
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the select expression.
|
2018-04-05 20:28:07 -04:00
|
|
|
|
var selectType = parent.GroupEnumerableType();
|
2018-04-05 20:29:50 -04:00
|
|
|
|
var selectExpression = Expression.Call(typeof(Enumerable), "Select",
|
|
|
|
|
new Type[] { selectType, groupExpression.Type },
|
|
|
|
|
parentExpression, groupExpressionLambda);
|
|
|
|
|
currentExpression = selectExpression;
|
2018-04-05 20:28:07 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (group.Parent == null)
|
|
|
|
|
{
|
2018-04-05 20:29:50 -04:00
|
|
|
|
currentExpression = Expression.Lambda(currentExpression, group.Parameter);
|
2018-04-05 20:28:07 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parent = group.Parent;
|
2018-04-10 20:00:40 -04:00
|
|
|
|
var parentExpression = CompileGroup(parent, NullHandling);
|
2018-04-05 20:28:07 -04:00
|
|
|
|
var selectType = parent.GroupEnumerableType();
|
2018-04-10 20:08:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (NullHandling != SelectNullHandling.LeaveAsIs)
|
|
|
|
|
{
|
|
|
|
|
if (group.Pieces.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
var path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(T => T.Name));
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 20:29:50 -04:00
|
|
|
|
var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter);
|
|
|
|
|
currentExpression = Expression.Call(typeof(Enumerable), "Select",
|
|
|
|
|
new Type[] { selectType, currentExpression.Type },
|
2018-04-05 20:28:07 -04:00
|
|
|
|
parentExpression, currentExpressionLambda);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-10 19:26:44 -04:00
|
|
|
|
|
|
|
|
|
Result = currentExpression;
|
2018-04-05 20:28:07 -04:00
|
|
|
|
}
|
2018-04-02 12:47:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|