I wanna parse Expression<Func<Entity, bool>>
to a model :
public class Program
{
static void Main()
{
List<Entity> entities = new List<Entity>
{
new Entity{ Id = 1, FullName = "Mohammad Dayyan"},
new Entity{ Id = 2, FullName = "Zahra Ahmadi"},
new Entity{ Id = 3, FullName = "Milad Ahmadi"},
new Entity{ Id = 4, FullName = "Ali Dayyan"},
new Entity{ Id = 5, FullName = "Masoud Ahmadi"},
};
//PredicateBuilder is from LinqKit http://www.albahari.com/nutshell/linqkit.aspx
Expression<Func<Entity, bool>> predicate2 = PredicateBuilder.False<Entity>();
predicate2 = predicate2.Or(q => q.Id == 1);
predicate2 = predicate2.Or(q => q.Id == 3);
predicate2 = predicate2.Or(q => q.Id == 5);
Expression<Func<Entity, bool>> predicate1 = PredicateBuilder.True<Entity>();
predicate1 = predicate1.And(q => q.FullName.Contains("Milad"));
//Predicate3 == {((True AndAlso Invoke(q => q.FullName.Contains("Milad"), f)) AndAlso Invoke(f => (((False OrElse (f.Id == 1)) OrElse (f.Id == 3)) OrElse (f.Id == 5)), f))}
Expression<Func<Entity, bool>> predicate3 = predicate1.And(predicate2);
ExpressionModel model = ParseExpression(predicate3);
Console.WriteLine(model);
Console.ReadKey();
}
static ExpressionModel ParseExpression(Expression<Func<Entity, bool>> expression)
{
BinaryExpression binaryExpression = expression.Body as BinaryExpression;
if (binaryExpression == null) return null;
Expression left = binaryExpression.Left;
Expression right = binaryExpression.Right;
ExpressionModel expressionModel = new ExpressionModel
{
NodeType = binaryExpression.NodeType,
Left = ParseExpression(left),
Right = ParseExpression(right)
};
return expressionModel;
}
static ExpressionModel ParseExpression(Expression expression)
{
BinaryExpression binaryExpression = expression as BinaryExpression;
if (binaryExpression == null) return null;
Expression left = binaryExpression.Left;
Expression right = binaryExpression.Right;
ExpressionModel expressionModel = new ExpressionModel
{
NodeType = binaryExpression.NodeType,
Left = ParseExpression(left),
Right = ParseExpression(right)
};
return expressionModel;
}
}
[Serializable]
public class Entity
{
public int Id { get; set; }
public string FullName { get; set; }
}
[Serializable]
public class ExpressionModel
{
public ExpressionModel Left { get; set; }
public ExpressionModel Right { get; set; }
public ExpressionType NodeType { get; set; }
}
But when parser reach to True
or Invoke(q => q.FullName.Contains("Milad"), f)
ParseExpression
method return null!
How can I resolve it?
Because this
Invoke(q => q.FullName.Contains("Milad"), f)
is a MethodCallExpression and thisreturns null, beacuse it is not a BinaryExpression.
Moreover
True
is a ConstantExpression. You need more complex code to parse it and handle all this options.Try to use this: metalinq