R – Subsonic dynamic query expression

dynamicexpressionsubsonic

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 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

Best Answer

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.