optional-parameterssubsonic2.2option-type

Subsonic - query with optional parameters


Using C# 3.5 through VS 2008 and subsonic 2.2. Anyone know if it's possible to create a subsonic query that essentially has an 'IF' in the middle of it, depending on whether a passed parameter was, for example, greater than zero.

For example, a delete method that has two passed parameters - A and B.

I want something like (pseudo code)

DELETE from Products
Where productId = A
if(B > 0)
{
AND ProductAttributeId = B
}

Obviously it wouldn't need the actual 'IF' clause in there but that's the essence of what I'm trying to do with subsonic. I know I can just have two different queries depending on whether the parameter is there or not but I was wondering if there's a cleaner way of doing it.

Thanks.


Solution

  • That's how I usually do it - it's not two queries, but one SqlQuery with optionally added constraints:

    SqlSquery q = DAL.DB.Delete()
        .From<DAL.Product()
        .Where(DAL.Product.ProductIdColumn).IsEqualTo(A);
    if (B > 0)
    {
        q.And(DAL.Product.ProductAttributeIdColumn).IsEqualTo(B);
    }
    q.Execute();
    

    There may be a typo, I can't test this right now.