user3501613
user3501613

Reputation: 610

Create LINQ expression Tree with multiple OR and AND condition

I want to generate LINQ expression tree with nested AND or OR condition. I have bellow expressions

    var constant = Expression.Constant("Jhon");
    var property = Expression.Property(paramExpr,"FirstName");
    var expression = Expression.Equal(property, constant);
    constant = Expression.Constant("12");
    property = Expression.Property(paramExpr, "Age");
    var expression2 = Expression.Equal(property, constant);

    expressionMain1 = Expression.AND(expression, expression2);

    constant = Expression.Constant("Mathew");
    property = Expression.Property(paramExpr,"LastName");
    expression = Expression.Equal(property, constant);
    constant = Expression.Constant("19");
    property = Expression.Property(paramExpr, "Age");
    expression2 = Expression.Equal(property, constant);

    expressionMain2 = Expression.And(expression, expression2);

I want the final lambda query like

(FirstName='John' AND Age='12') OR (LastName='Mathew' AND Age= '19')

Upvotes: 2

Views: 2698

Answers (1)

Steve Harris
Steve Harris

Reputation: 5109

Would this do it?

Expression.OrElse(expressionMain1, expressionMain2)

Upvotes: 2

Related Questions