dotnet-dynamic-linq/PoweredSoft.DynamicLinq.ConsoleApp/BetterProto2.cs

195 lines
8.6 KiB
C#
Raw Normal View History

2018-04-02 12:47:30 -04:00
using PoweredSoft.DynamicLinq.Dal.Pocos;
using PoweredSoft.DynamicLinq.Helpers;
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()
{
//Case1();
Case2();
}
public static void Case1()
2018-04-02 12:47:30 -04:00
{
// the expression parser.
2018-04-02 12:47:30 -04:00
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;
2018-04-02 12:47:30 -04:00
}
public static void Case2()
{
// the expression parser.
var ep = new ExpressionParser(typeof(Author), "Posts.Author.Posts.Author.Website.Url");
new List<Author>().AsQueryable().Select(t => new
{
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-02 16:01:35 -04:00
new List<Post>().AsQueryable().Select(t => new
{
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))
});
// the builder.
var per = new PathExpressionResolver(ep);
per.NullHandling = SelectNullHandling.Handle;
per.Resolve();
// the result expression.
var result = per.Result;
}
2018-04-02 12:47:30 -04:00
}
2018-04-02 12:47:30 -04:00
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; }
2018-04-10 20:00:40 -04:00
public Type NullType { get; set; }
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;
}
public void Resolve()
{
Result = null;
// parse the expression.
Parser.Parse();
// group the piece by common parameters
var groups = Parser.GroupBySharedParameters();
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;
groups.ReversedForEach(group =>
{
2018-04-05 20:29:50 -04:00
if (currentExpression == null)
{
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);
if (group.Parent == null)
{
2018-04-05 20:29:50 -04:00
currentExpression = groupExpressionLambda;
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.
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;
}
else
{
if (group.Parent == null)
{
2018-04-05 20:29:50 -04:00
currentExpression = Expression.Lambda(currentExpression, group.Parameter);
return;
}
var parent = group.Parent;
2018-04-10 20:00:40 -04:00
var parentExpression = CompileGroup(parent, NullHandling);
var selectType = parent.GroupEnumerableType();
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 },
parentExpression, currentExpressionLambda);
}
});
2018-04-10 19:26:44 -04:00
Result = currentExpression;
}
2018-04-02 12:47:30 -04:00
}
}