-
Notifications
You must be signed in to change notification settings - Fork 302
Query Single Object
Lee Grissom edited this page Jun 13, 2017
·
4 revisions
Selecting an object from the database can be done in a few different ways.
The easiest way to load an object from the database is by passing the primary key to the SingleById<T>()
method.
IDatabase db = new Database("connStringName");
User u = db.SingleById<User>(3);
Below you can see that only the where is specified. If you don't explicitly supply the select clause it will be automatically generated for you and the where will then be appended.
User u = db.Single<User>("where emailaddress = @0", "[email protected]");
or
User u = db.Single<User>("select u.* from users u where emailaddress = @0", "[email protected]");
Both these methods have a 'OrDefault' method if you are unsure that the object will exist. If it doesn't exist and you don't use the 'OrDefault' override it will throw an exception.
There are also First<T>
and FirstOrDefault<T>
which will not throw an exception if more than 1 record is returned.