Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Build Lambda Expression Tree with multiple conditions

Tags:

Note: I know it's much simple to create this using dynamic linq but I want to learn.

I want to create a lambda that "finds": Name=David AND Age=10.

 class Person     {        public int Age { get; set; }        public string Name { get; set; }     }      var lambda = LabmdaExpression<Person>("Name", "David", "Age", 10);  static Expression<Func<T, bool>> LabmdaExpression<T>(string property1, string value1, string property2, int value2) {       ParameterExpression parameterExpression = Expression.Parameter(typeof(Person), "o");      MemberExpression memberExpression1 = Expression.PropertyOrField(parameterExpression, property1);      MemberExpression memberExpression2 = Expression.PropertyOrField(parameterExpression, property2);       ConstantExpression valueExpression1 = Expression.Constant(value1, typeof(string));      ConstantExpression valueExpression2 = Expression.Constant(value2, typeof(int));       BinaryExpression binaryExpression1 = Expression.Equal(memberExpression1, valueExpression1);      BinaryExpression binaryExpression2 = Expression.Equal(memberExpression2, valueExpression2);       var ret1 =   Expression.Lambda<Func<T, bool>>(binaryExpression1, parameterExpression);      var ret2 =   Expression.Lambda<Func<T, bool>>(binaryExpression2, parameterExpression);      } 
like image 446
jullin Avatar asked Jun 09 '11 16:06

jullin


People also ask

What is more complex expression tree?

That's the basics of building an expression tree in memory. More complex trees generally mean more node types, and more nodes in the tree. Let's run through one more example and show two more node types that you will typically build when you create expression trees: the argument nodes, and method call nodes.

Can we use dynamic in lambda expression?

In 2010, the Dynamic Type was introduced and that gave us the ability to create dynamic lambda expressions.

What is AC expression tree?

Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y . You can compile and run code represented by expression trees.

What is expression trees and how they used in LINQ?

Expression Trees provide richer interaction with the arguments that are functions. You write function arguments, typically using Lambda Expressions, when you create LINQ queries. In a typical LINQ query, those function arguments are transformed into a delegate the compiler creates.


1 Answers

Expression andExpression = Expression.AndAlso(binaryExpression1, binaryExpression2);  return Expression.Lambda<Func<T, bool>>(andExpression , parameterExpression); 

Edit - comment

You just chain together all your expresions

so in order to get this expression X AND (Y OR (Z OR Q))

Expression ZorQ = Expression.OrElse(zExp, qExp); Expression YorZorQ = Expression.OrElse(yExp, ZorQ); Expression XandYorZorQ = Expression.AndAlso(xExp, YorZorQ); 
like image 96
Aducci Avatar answered Oct 14 '22 10:10

Aducci