views:

515

answers:

1

Hi all,

I am having a few issues with trying to live in a subsonic world and being a subsonic girl when it comes to subsonic expressions....

after reading http://stackoverflow.com/questions/765896/subsonic-query-conditiona-or-conditionb-and-conditionc it would appear i am not the only one with this sort of issue but hopefully someone (the almighty rob??) can answer this.

i am attempting to create an expression in my query based on a looping condition. what i want to achieve (in pseudo code) is something like this:

objQuery.andexpressionstart();

foreach (condition in conditions){

   if (condition){
      objQuery.and(conditionColumn).isequalto(X);
   }

}
objQuery.expressionstop();

my main issue is that each condition that is inside the expression is a different column - otherwise i could just use .In() . i also have extra search criteria (read a fair bit) outside so it can't be outside an expression.

i REALLY don't want to leave the warm coseyness of the strongly-typed-subsonic womb however i think in this instance i might have too... if i DO have to is there a way to add to a subsonic query with a hand typed condition so i don't have to change all the other code in the query (alot of business logic living in subsonic land right now)

As always, thanks for any help cheers

+3  A: 

I haven't the time to test this right now, but I think if you do something like the following should work:

bool isFirstCondition = true;

foreach (condition in conditions){

   if (condition)    
   {
        if(isFirstCondition)
        {
           objQuery.AndExpression(conditionColumn).isequalto(X);
           isFirstCondition = false;
        }
        else
        {  
           objQuery.and(conditionColumn).isequalto(X);

        }
   }

}

Make sure all your other conditions have been added prior to the loop.

ptutt
i have a problem in the fact that i have more than one "expression"
Doug
my understanding is that when you use "AndExpression" it will group everything until the next "AndExpression" is called.(first expression and (second expression and ( third expression )))where each expression can contain any number of criteria.Try it out and check what SQL is being generated.
ptutt
The above works awesome!... great work, thank you :)
Doug