How to create a nested expression

Posts   
 
    
zachdavis
User
Posts: 2
Joined: 16-Jul-2014
# Posted on: 16-Jul-2014 04:03:39   

Hi Everyone,

First timer here. Thanks in advance for all the valuable help.

I am trying to use expressions to create a complex query. I am wanting to end up with a where clause like this:


WHERE
  (tableName.UserId = 1)
  AND
  (
     tableTwo.SecondField like '%answer%'
     OR
     tableTwo.ThirdField like '%answer2%'
  )

I have tried using a Predicate Expression but I can't get the desired outcome.

What am I missing??

Thanks again for the help.

Thanks,

-zd

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Jul-2014 07:18:13   

Hi zd, and Welcome to the forums wink

About your question. You can either:

A. Use &, | and ()

IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) 
    | (Table2Fields.Bar2 == "Three");

or

B. Use .AddWithOr and .AddWithAnd methods of the PredicateExpression class.

IPredicateExpression filter1 = new PredicateExpression();
filter.Add(TableFields.Foo=="One");
filter.Add(Table1Fields.Bar == "Two");

IPredicateExpression filter2 = new PredicateExpression();
filter2.Add(Table2Fields.Bar2 == "Three");

IPredicateExpression mainFilter = new PredicateExpression();
mainFilter.Add(filter1);
mainFilter.AddWithOr(filter2);

or

C. Mixed:

IPredicateExpression filter1 = new PredicateExpression();
filter.Add(TableFields.Foo=="One" & Table1Fields.Bar == "Two");
filter.AddWithOr(Table2Fields.Bar2 == "Three");

For more info read Constructing Predicate Expressions in the documentation.

Hope that helps sunglasses

David Elizondo | LLBLGen Support Team
zachdavis
User
Posts: 2
Joined: 16-Jul-2014
# Posted on: 17-Jul-2014 17:00:07   

David,

This is exactly what I wanted. Works perfectly.

Thanks for the help.

-zd