well advanced expression parser.
This commit is contained in:
parent
14f1091eb8
commit
3a24d6086e
@ -14,20 +14,38 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var type = typeof(Dal.Pocos.Author);
|
||||
|
||||
|
||||
|
||||
var q = new List<Author>().AsQueryable();
|
||||
q.Select(t => new
|
||||
{
|
||||
Ids = t.Posts.SelectMany(t2 => t2.Comments.Select(t3 => t3.CommentLikes))
|
||||
});
|
||||
|
||||
var expressionParser = new ExpressionParser(typeof(Author), "Posts.Comments.Post.Author");
|
||||
expressionParser.Parse();
|
||||
var result = expressionParser.Groups;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* var type = typeof(Dal.Pocos.Author);
|
||||
var param = Expression.Parameter(type);
|
||||
var parts = ExpressionPathPart.Split(param, "Posts.Comments.Id");
|
||||
|
||||
*
|
||||
// add the last part.
|
||||
var parent = parts.Last();
|
||||
var toListType = parent.GetToListType();
|
||||
var parent = parts.First();
|
||||
var last = parts.Last();
|
||||
var toListType = last.GetToListType();
|
||||
|
||||
parts.Add(new ExpressionPathPart
|
||||
{
|
||||
ParentExpression = parent.PartExpression,
|
||||
PartExpression = Expression.Call(typeof(Enumerable), "ToList", new[] { toListType }, parent.ParentExpression)
|
||||
});
|
||||
PartExpression = Expression.Call(typeof(Enumerable), "ToList", new[] { toListType }, parent.PartExpression)
|
||||
});*/
|
||||
|
||||
var result = CreateSelectExpressionFromParts(parts, SelectCollectionHandling.Flatten, SelectNullHandling.New);
|
||||
//var result = CreateSelectExpressionFromParts(parts, SelectCollectionHandling.Flatten, SelectNullHandling.New);
|
||||
}
|
||||
|
||||
public static Expression CreateSelectExpressionFromParts(List<ExpressionPathPart> parts,
|
||||
|
@ -5,6 +5,107 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Helpers
|
||||
{
|
||||
public class ExpressionParameterGroup
|
||||
{
|
||||
public ExpressionParameterGroup Parent { get; set; }
|
||||
public ParameterExpression Parameter { get; set; }
|
||||
public List<ExpressionPiece> Parts { get; set; } = new List<ExpressionPiece>();
|
||||
|
||||
public ExpressionParameterGroup(ParameterExpression parameter)
|
||||
{
|
||||
Parameter = parameter;
|
||||
}
|
||||
|
||||
public void AddSubPart(ExpressionPiece expressionPart)
|
||||
{
|
||||
Parts.Add(expressionPart);
|
||||
}
|
||||
}
|
||||
|
||||
public class ExpressionPiece
|
||||
{
|
||||
public ExpressionParameterGroup Parameter { get; set; }
|
||||
public ExpressionPiece Parent { get; set; }
|
||||
public Expression Part { get; set; }
|
||||
|
||||
public ExpressionPiece(ExpressionParameterGroup parameter, ExpressionPiece parent = null)
|
||||
{
|
||||
Parameter = parameter;
|
||||
Parent = parent;
|
||||
}
|
||||
|
||||
public void Resolve(string piece)
|
||||
{
|
||||
Part = Expression.PropertyOrField(Parent?.Part ?? Parameter.Parameter, piece);
|
||||
}
|
||||
|
||||
public bool IsGenericEnumerable() => QueryableHelpers.IsGenericEnumerable(Part);
|
||||
public Type GetGenericEnumerableType() => Part.Type.GenericTypeArguments.First();
|
||||
}
|
||||
|
||||
public class ExpressionParser
|
||||
{
|
||||
public ParameterExpression Parameter { get; protected set; }
|
||||
public string Path { get; protected set; }
|
||||
public List<ExpressionParameterGroup> Groups { get; set; } = new List<ExpressionParameterGroup>();
|
||||
|
||||
public ExpressionParser(Type type, string path) : this(Expression.Parameter(type), path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ExpressionParser(ParameterExpression parameter, string path)
|
||||
{
|
||||
if (parameter == null)
|
||||
throw new ArgumentNullException("parameter");
|
||||
|
||||
if (string.IsNullOrEmpty(path))
|
||||
throw new ArgumentNullException("path");
|
||||
|
||||
Parameter = parameter;
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public void Parse()
|
||||
{
|
||||
Groups = new List<ExpressionParameterGroup>();
|
||||
var pieces = Path.Split('.').ToList();
|
||||
|
||||
// create the current parameter.
|
||||
var currentGroup = CreateAndAddParameterGroup(Parameter);
|
||||
ExpressionPiece parentPiece = null;
|
||||
|
||||
pieces.ForEach(piece =>
|
||||
{
|
||||
var expressionPiece = new ExpressionPiece(currentGroup, parentPiece);
|
||||
expressionPiece.Resolve(piece);
|
||||
currentGroup.AddSubPart(expressionPiece);
|
||||
|
||||
if (expressionPiece.IsGenericEnumerable())
|
||||
{
|
||||
var param = Expression.Parameter(expressionPiece.GetGenericEnumerableType());
|
||||
currentGroup = CreateAndAddParameterGroup(param, currentGroup);
|
||||
parentPiece = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
parentPiece = expressionPiece;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public ExpressionParameterGroup CreateAndAddParameterGroup(ParameterExpression parameter, ExpressionParameterGroup parent = null)
|
||||
{
|
||||
var group = new ExpressionParameterGroup(parameter);
|
||||
|
||||
if (parent != null)
|
||||
group.Parent = parent;
|
||||
|
||||
Groups.Add(group);
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
||||
public class ExpressionPathPart
|
||||
{
|
||||
public Expression ParentExpression { get; set; }
|
||||
@ -54,5 +155,6 @@ namespace PoweredSoft.DynamicLinq.Helpers
|
||||
}
|
||||
|
||||
public Type ParentGenericEnumerableType() => ParentExpression.Type.GenericTypeArguments.First();
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user