@@ -281,20 +281,19 @@ db.Select<Author>(q => q.Rate == 10 && q.City == "Mexico");
281
281
OrmLite also includes a number of convenient API's providing DRY, typed data access for common queries:
282
282
283
283
``` csharp
284
- Person personById = db .SingleById <Person >(1 );
284
+ Person person = db .SingleById <Person >(1 );
285
285
```
286
286
287
287
``` csharp
288
- Person personByAge = db .Single <Person >(x => x .Age == 42 );
288
+ Person person = db .Single <Person >(x => x .Age == 42 );
289
289
```
290
290
291
291
``` csharp
292
- int maxAgeUnder50 = db .Scalar <Person , int >(x => Sql .Max (x .Age ), x => x .Age < 50 );
293
- ```
292
+ var q = db .From <Person >()
293
+ .Where (q => q .Age > 40 )
294
+ .Select (Sql .Count (" *" ));
294
295
295
- ``` csharp
296
- int peopleOver40 = db .Scalar <int >(
297
- db .From <Person >().Select (Sql .Count (" *" )).Where (q => q .Age > 40 ));
296
+ int peopleOver40 = db .Scalar <int >(q );
298
297
```
299
298
300
299
``` csharp
@@ -306,23 +305,39 @@ bool has42YearOlds = db.Exists<Person>(new { Age = 42 });
306
305
```
307
306
308
307
``` csharp
309
- List < string > results = db .Column <string >(db .From <Person >().Select (x => x .LastName )
310
- .Where (q => q .Age == 27 ));
308
+ int maxAgeUnder50 = db .Scalar <Person , int >(x => Sql .Max (x .Age ), x => x .Age < 50 );
309
+ ```
310
+
311
+ ``` csharp
312
+ var q = db .From <Person >()
313
+ .Where (q => q .Age == 27 )
314
+ .Select (x => x .LastName );
315
+
316
+ List < string > results = db .Column <string >(q );
311
317
```
312
318
313
319
``` csharp
314
- HashSet < int > results = db .ColumnDistinct <int >(db .From <Person >().Select (x => x .Age )
315
- .Where (q => q .Age < 50 ));
320
+ var q = db .From <Person >()
321
+ .Where (q => q .Age < 50 )
322
+ .Select (x => x .Age );
323
+
324
+ HashSet < int > results = db .ColumnDistinct <int >(q );
316
325
```
317
326
318
327
``` csharp
319
- Dictionary < int ,string > results = db .Dictionary <int , string >(
320
- db .From <Person >().Select (x => new { x .Id , x .LastName }).Where (x => x .Age < 50 ));
328
+ var q = db .From <Person >()
329
+ .Where (x => x .Age < 50 )
330
+ .Select (x => new { x .Id , x .LastName });
331
+
332
+ Dictionary < int ,string > results = db .Dictionary <int , string >(q );
321
333
```
322
334
323
335
``` csharp
324
- Dictionary < int , List < string >> results = db .Lookup <int , string >(
325
- db .From <Person >().Select (x => new { x .Age , x .LastName }).Where (q => q .Age < 50 ));
336
+ var q = db .From <Person >()
337
+ .Where (q => q .Age < 50 )
338
+ .Select (x => new { x .Age , x .LastName });
339
+
340
+ Dictionary < int , List < string >> results = db .Lookup <int , string >(q );
326
341
```
327
342
328
343
### INSERT, UPDATE and DELETEs
@@ -332,10 +347,10 @@ To see the behaviour of the different APIs, all examples uses this simple model
332
347
``` csharp
333
348
public class Person
334
349
{
335
- public int Id { get ; set ; }
336
- public string FirstName { get ; set ; }
337
- public string LastName { get ; set ; }
338
- public int ? Age { get ; set ; }
350
+ public int Id { get ; set ; }
351
+ public string FirstName { get ; set ; }
352
+ public string LastName { get ; set ; }
353
+ public int ? Age { get ; set ; }
339
354
}
340
355
```
341
356
@@ -393,21 +408,21 @@ db.UpdateOnly(new Person { FirstName = "JJ", LastName = "Hendo" },
393
408
Using a typed SQL Expression:
394
409
395
410
``` csharp
396
- db .UpdateOnly ( new Person { FirstName = " JJ " },
397
- onlyFields : q => q . Update ( p => p . FirstName ). Where ( x => x . FirstName == " Jimi " ) );
411
+ var q = db .From < Person >(). Update ( p => p . FirstName ). Where ( x => x . FirstName == " Jimi " );
412
+ db . UpdateOnly ( new Person { FirstName = " JJ " }, onlyFields : q );
398
413
```
399
414
400
415
For the ultimate flexibility we also provide un-typed, string-based expressions. Use the ` .SqlFmt() ` extension method escape parameters (inspired by [ massive] ( https://github.com/robconery/massive ) ):
401
416
402
417
``` csharp
403
418
db .UpdateFmt <Person >(set : " FirstName = {0}" .SqlFmt (" JJ" ),
404
- where : " LastName = {0}" .SqlFmt (" Hendrix" ));
419
+ where : " LastName = {0}" .SqlFmt (" Hendrix" ));
405
420
```
406
421
Even the Table name can be a string so you perform the same update without requiring the Person model at all:
407
422
408
423
``` csharp
409
424
db .UpdateFmt (table : " Person" , set : " FirstName = {0}" .SqlFmt (" JJ" ),
410
- where : " LastName = {0}" .SqlFmt (" Hendrix" ));
425
+ where : " LastName = {0}" .SqlFmt (" Hendrix" ));
411
426
```
412
427
### INSERT
413
428
@@ -431,7 +446,8 @@ db.Delete<Person>(p => p.Age == 27);
431
446
432
447
Or an Expression Visitor:
433
448
``` csharp
434
- db .Delete <Person >(q => q .Where (p => p .Age == 27 ));
449
+ var q = db .From <Person >().Where (p => p .Age == 27 );
450
+ db .Delete <Person >(q );
435
451
```
436
452
437
453
As well as un-typed, string-based expressions:
@@ -565,7 +581,6 @@ var topVIPs = db.SelectLazyFmt<Person>("Age > {0}", 40).Where(p => IsVip(p)).Tak
565
581
- `Save ` and `SaveAll ` will Insert if no record with **Id** exists, otherwise it Updates.
566
582
- Methods containing the word **Each** return an IEnumerable<T > and are lazily loaded (i.e. non-buffered).
567
583
568
-
569
584
# Features
570
585
571
586
Whilst OrmLite aims to provide a light-weight typed wrapper around SQL, it offers a number of convenient features that makes working with RDBMS's a clean and enjoyable experience:
0 commit comments