2018-04-02 12:47:30 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Dal.Pocos;
|
|
|
|
|
using PoweredSoft.DynamicLinq.Helpers;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ExpressionParserPiece
|
|
|
|
|
{
|
|
|
|
|
public ParameterExpression Parameter { get; set; }
|
|
|
|
|
public MemberExpression MemberExpression { get; set; }
|
|
|
|
|
public ExpressionParserPiece Parent { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool IsGenericEnumerable => QueryableHelpers.IsGenericEnumerable(MemberExpression);
|
|
|
|
|
public Type EnumerableType => MemberExpression.Type.GenericTypeArguments.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public class PathExpressionResolver
|
|
|
|
|
{
|
|
|
|
|
public SelectNullHandling NullChecking { 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()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 12:47:30 -04:00
|
|
|
|
public class ExpressionParser
|
|
|
|
|
{
|
|
|
|
|
public ParameterExpression Parameter { get; protected set; }
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
public List<ExpressionParserPiece> Pieces { get; set; } = new List<ExpressionParserPiece>();
|
|
|
|
|
|
|
|
|
|
public ExpressionParser(Type type, string path) : this(Expression.Parameter(type), path)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExpressionParser(ParameterExpression parameter, string path)
|
|
|
|
|
{
|
|
|
|
|
Parameter = parameter;
|
|
|
|
|
Path = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Parse()
|
|
|
|
|
{
|
|
|
|
|
Pieces.Clear();
|
|
|
|
|
|
|
|
|
|
var param = Parameter;
|
|
|
|
|
var pathPieces = Path.Split('.').ToList();
|
|
|
|
|
ExpressionParserPiece parent = null;
|
|
|
|
|
|
|
|
|
|
pathPieces.ForEach(pp =>
|
|
|
|
|
{
|
|
|
|
|
var memberExpression = Expression.PropertyOrField(param, pp);
|
|
|
|
|
var current = new ExpressionParserPiece
|
|
|
|
|
{
|
|
|
|
|
Parameter = param,
|
|
|
|
|
MemberExpression = memberExpression,
|
|
|
|
|
Parent = parent
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Pieces.Add(current);
|
|
|
|
|
param = ResolveNextParam(current);
|
|
|
|
|
parent = current;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ParameterExpression ResolveNextParam(ExpressionParserPiece current)
|
|
|
|
|
{
|
|
|
|
|
var type = current.IsGenericEnumerable ? current.EnumerableType : current.MemberExpression.Type;
|
|
|
|
|
var result = Expression.Parameter(type);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|