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

118 lines
3.9 KiB
C#

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()
{
// 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 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();
}
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()
{
}
}
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;
}
}
}