tags:

views:

38

answers:

2

Is it possible to apply a WHERE clause on a SubSonic query?

For example, I get get a single based on id...

db.Single<Storage>(id);

But how can I get a single based on a simple WHERE clause?

db.Single<Storage>(WHERE columnname == "value");
+1  A: 

that's possible:

// Will return a Storage instance with property IsNew = true, if record does not exist
// since an object created with new never can be null
var storage1 = new Storage(1); // id = 1
var storage1 = new Storage(Storag.Columns.ColumnName, "value");

// Will return 0 if record not found (subsonic3 only)
var storage3 = (from s in Storage
               where s.ColumnName == "value"
               select s).SingleOrDefault();

// Will throw an exception if record not found  (subsonic3 only)
var storage3 = (from s in Storage
               where s.ColumnName == "value"
               select s).Single();   

Since db is a partial class you can extend it. Just create a new File within the same namespace (but another folder in your solution). This applies to subsonic 2 but will be similar to subsonic 3, I think.

public static partial class DB
{
    public static T Single<T>(String columName, Object columnValue) where T: RecordBase<T>, new()
    {
        return Select().From<T>()
                       .Where(columnName).IsEqualTo(columnValue)
                       .ExecuteSingle<T>();
    }
}
SchlaWiener
+1  A: 

Thanks for the above, this was a help and eventually I simplified this to the below...

        db.Single<Storage>(s => s.ColumnName == "value");
Desiny
Yeah, didn't thought about that ;) Still using SubSonic 2 because I am restricted to framework 2.0. Looking forward to mirgrate in the near future.
SchlaWiener
@SchlaWiener: Off-topic, but since you mention it, I will say that I haven't been too happy with SubSonic 3. Sure the LINQ syntax is nice, but it's buggy, and when the LINQ provider isn't hitting a bug, it's just plain limited. Not to mention there's just enough breaking changes in the templated code to be a hassle. I hope you can move up in framework's soon, but don't get tooo excited for SS3, lol.
qstarin
Since I am using mysql which is probably not as much tested as sql server, I am expecting to find some bugs. But since it's open source I can fix it myself ;) The killer feature for me is subsonic in conjunction with XtraGrid's Server mode and linq data source http://tv.devexpress.com/#XtraGridLinqServerMode I have a demo app running and it's awesome. I can do incremental search / sorting and grouping on a table with 200000+ records and it's damn fast.
SchlaWiener
@qstarin - for me the database generation from classes saves so much that any of the downsides can be forgiven. I dont think that there is an alternative until m$ build this feature into the next entity framework?
Desiny
@Desiny: There's only about 10 different tools for .Net like SubSonic, that generate data access layers off your database, and several them actually support MySQL as a first-class DB. @SchlaWiener: Except that SubSonic v3 has already seen multiple bugs reintroduced to the codebase and has outstanding issues that active devs leave open while working on, say, Oracle support. It is, of course, everyone's own choice.
qstarin