2

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')
user3501613
  • 596
  • 7
  • 28

1 Answers1

2

Would this do it?

Expression.OrElse(expressionMain1, expressionMain2)
Steve Harris
  • 5,014
  • 1
  • 10
  • 25
  • this will not add Brackets in between 2 main AND queries and the result will be different – user3501613 Apr 15 '21 at 08:20
  • 1
    What would you need brackets for? The ANDs will be evaluated and then the ORs. Brackets would make no difference. I assure you if it was the other way around then 'brackets' would be expected but this is an expression tree so even then it would still work. – Steve Harris Apr 15 '21 at 08:22
  • I think this both query (FirstName='John' AND Age='12') OR (LastName='Mathew' AND Age= '19') FirstName='John' AND Age='12' OR LastName='Mathew' AND Age= '19' Will give different answer – user3501613 Apr 15 '21 at 08:23
  • 1
    I'm pretty sure that is the same thing and will not give a different answer! – Steve Harris Apr 15 '21 at 08:24
  • so if the query is in other way like (FirstName='John' OR Age='12') AND (LastName='Mathew' OR Age= '19') then how can I achieve that. – user3501613 Apr 15 '21 at 08:27
  • 1
    Yes the brackets matter in this case, however being an expression tree rather than typed code, the hierarchy will be automatically assumed. So you would create your two OR expressions and wrap them in an AND expression. The result will be correct. – Steve Harris Apr 15 '21 at 08:29
  • so for this scenario (FirstName='John' OR Age='12') AND (LastName='Mathew' OR Age= '19') if I use Expression.AND(expressionMain1, expressionMain2) will work ? – user3501613 Apr 15 '21 at 08:31
  • can you please look into this question https://stackoverflow.com/questions/67104241/generate-dynamic-linq-expression-by-iterating-object-properties and if you have any suggestion please provide – user3501613 Apr 15 '21 at 08:32
  • See Wiki : https://en.wikipedia.org/wiki/Binary_expression_tree – jdweng Apr 15 '21 at 08:36
  • @SteveHarris, it is beginners bug. It should be `Expression.OrElse`. – Svyatoslav Danyliv Apr 15 '21 at 16:13
  • Yes, AndAlso and OrElse – Steve Harris Apr 15 '21 at 16:46