tags:

views:

189

answers:

2

Before 2.1 I was able to write code like this (using the AddWhere):

Query q = new Query(Search.Schema);
q.BuildSelectCommand();
if (!String.IsNullOrEmpty(barcode)) q.AddWhere("Barcode", Comparison.Like, "%" +     barcode.Trim() + "%");
if (!String.IsNullOrEmpty(fromDate)) q.AddWhere("FromDate", Comparison.GreaterOrEquals, fromDate);
if (!String.IsNullOrEmpty(toDate)) q.AddWhere("ToDate", Comparison.LessOrEquals, fromDate);
if (!String.IsNullOrEmpty(department)) q.AddWhere("DeptNo", Comparison.Like, "%" + department.Trim() + "%");
if (!String.IsNullOrEmpty(series)) q.AddWhere("SeriesNo", Comparison.Like, "%" + series.Trim() + "%");
if (!String.IsNullOrEmpty(altcode)) q.AddWhere("AltCode", Comparison.Like, "%" + altcode.Trim() + "%");

This allowed me to create nice search forms and only search on variables that users entered into the form, I am trying to reproduce this functionality in a simple manner with 2.1 and can't figure it out. Does anyone have any suggestions?

A: 
IDataReader rdr=new Select().From<Search>().Where("barcode").Like(..)
.And("FromDate").GreaterThan(..)
.And("ToDate").LessOrEqualTo(..)
.And("DeptNo").Like()
...
.ExecuteReader();

http://blog.wekeroad.com/2008/01/10/subsonic-version-21-pakala-preview-the-new-query-tool/

Now, if you're adventurous, you can use 3.0 (which is in the works) and you can use Linq to do this:

http://code.google.com/p/subsonicthree/

I'm happy to help here...

Rob Conery
Wow, that was quick, and at such a late hour. Not perfectly what I am looking for because there is never a guaranteed WHERE, you start off with Barcode, but the may search on one of the other values, so I don't want Barcode in the query. With the old AddWhere method you could be more dynamic with the where statements.
Oh, and I can't use 3.0, because the client environment is an Intranet where the IT department has not installed anything above .Net 2.0, so I have to have a 2.0 solution.But, thank you for responding
+2  A: 

the solution I came up with is:

SqlQuery q = new Select("syKey").From<Search>().Where("1").IsEqualTo("1");
if (!String.IsNullOrEmpty(barcode)) q.And("Barcode").Like("%" + barcode.Trim() + "%");
if (!String.IsNullOrEmpty(fromDate)) q.And("FromDate").IsGreaterThanOrEqualTo(fromDate);
if (!String.IsNullOrEmpty(toDate)) q.And("ToDate").IsLessThanOrEqualTo(fromDate);
if (!String.IsNullOrEmpty(department)) q.And("DeptNo").Like("%" + department.Trim() + "%");
if (!String.IsNullOrEmpty(series)) q.And("SeriesNo").Like("%" + series.Trim() + "%");
if (!String.IsNullOrEmpty(altcode)) q.And("AltCode").Like("%" + altcode.Trim() + "%");

Notice with the default Where("1").IsEqualTo("1") I can dynamically add "And" to the query to make it work. It would be nice to see AddWhere brought back though so you can keep stacking WHERE commands on top of each other.