@@ -235,7 +235,7 @@ in a single SQL Query, e.g:
235
235
``` csharp
236
236
var usaCustomerIds = db .From <Customer >(c => c .Country == " USA" ).Select (c => c .Id );
237
237
var usaCustomerOrders = db .Select (db .From <Order >()
238
- .Where (q => Sql .In (q .CustomerId , usaCustomerIds )));
238
+ .Where (x => Sql .In (x .CustomerId , usaCustomerIds )));
239
239
```
240
240
241
241
# API Examples
@@ -248,32 +248,32 @@ construct more complex queries. To give you a flavour here are some examples:
248
248
249
249
``` csharp
250
250
int agesAgo = DateTime .Today .AddYears (- 20 ).Year ;
251
- db .Select <Author >(q => q .Birthday >= new DateTime (agesAgo , 1 , 1 )
252
- && q .Birthday <= new DateTime (agesAgo , 12 , 31 ));
251
+ db .Select <Author >(x => x .Birthday >= new DateTime (agesAgo , 1 , 1 )
252
+ && x .Birthday <= new DateTime (agesAgo , 12 , 31 ));
253
253
```
254
254
255
255
``` csharp
256
- db .Select <Author >(q => Sql .In (q .City , " London" , " Madrid" , " Berlin" ));
256
+ db .Select <Author >(x => Sql .In (x .City , " London" , " Madrid" , " Berlin" ));
257
257
```
258
258
259
259
``` csharp
260
- db .Select <Author >(q => q .Earnings <= 50 );
260
+ db .Select <Author >(x => x .Earnings <= 50 );
261
261
```
262
262
263
263
``` csharp
264
- db .Select <Author >(q => q .Name .StartsWith (" A" ));
264
+ db .Select <Author >(x => x .Name .StartsWith (" A" ));
265
265
```
266
266
267
267
``` csharp
268
- db .Select <Author >(q => q .Name .EndsWith (" garzon" ));
268
+ db .Select <Author >(x => x .Name .EndsWith (" garzon" ));
269
269
```
270
270
271
271
``` csharp
272
- db .Select <Author >(q => q .Name .Contains (" Benedict" ));
272
+ db .Select <Author >(x => x .Name .Contains (" Benedict" ));
273
273
```
274
274
275
275
``` csharp
276
- db .Select <Author >(q => q .Rate == 10 && q .City == " Mexico" );
276
+ db .Select <Author >(x => x .Rate == 10 && x .City == " Mexico" );
277
277
```
278
278
279
279
### Convenient common usage data access patterns
@@ -290,7 +290,7 @@ Person person = db.Single<Person>(x => x.Age == 42);
290
290
291
291
``` csharp
292
292
var q = db .From <Person >()
293
- .Where (q => q .Age > 40 )
293
+ .Where (x => x .Age > 40 )
294
294
.Select (Sql .Count (" *" ));
295
295
296
296
int peopleOver40 = db .Scalar <int >(q );
@@ -310,15 +310,15 @@ int maxAgeUnder50 = db.Scalar<Person, int>(x => Sql.Max(x.Age), x => x.Age < 50)
310
310
311
311
``` csharp
312
312
var q = db .From <Person >()
313
- .Where (q => q .Age == 27 )
313
+ .Where (x => x .Age == 27 )
314
314
.Select (x => x .LastName );
315
315
316
316
List < string > results = db .Column <string >(q );
317
317
```
318
318
319
319
``` csharp
320
320
var q = db .From <Person >()
321
- .Where (q => q .Age < 50 )
321
+ .Where (x => x .Age < 50 )
322
322
.Select (x => x .Age );
323
323
324
324
HashSet < int > results = db .ColumnDistinct <int >(q );
@@ -334,7 +334,7 @@ Dictionary<int,string> results = db.Dictionary<int, string>(q);
334
334
335
335
``` csharp
336
336
var q = db .From <Person >()
337
- .Where (q => q .Age < 50 )
337
+ .Where (x => x .Age < 50 )
338
338
.Select (x => new { x .Age , x .LastName });
339
339
340
340
Dictionary < int , List < string >> results = db .Lookup <int , string >(q );
@@ -401,14 +401,19 @@ db.UpdateOnly(new Person { FirstName = "JJ" },
401
401
Instead of using the expression filters above you can choose to use an ExpressionVisitor builder which provides more flexibility when you want to programatically construct the update statement:
402
402
403
403
``` csharp
404
- db .UpdateOnly (new Person { FirstName = " JJ" , LastName = " Hendo" },
405
- onlyFields : q => q .Update (p => p .FirstName ));
404
+ var q = db .From <Person >()
405
+ .Update (p => p .FirstName );
406
+
407
+ db .UpdateOnly (new Person { FirstName = " JJ" , LastName = " Hendo" }, onlyFields : q );
406
408
```
407
409
408
410
Using a typed SQL Expression:
409
411
410
412
``` csharp
411
- var q = db .From <Person >().Update (p => p .FirstName ).Where (x => x .FirstName == " Jimi" );
413
+ var q = db .From <Person >()
414
+ .Where (x => x .FirstName == " Jimi" )
415
+ .Update (p => p .FirstName );
416
+
412
417
db .UpdateOnly (new Person { FirstName = " JJ" }, onlyFields : q );
413
418
```
414
419
@@ -434,7 +439,10 @@ db.Insert(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 2
434
439
But do provide an API that takes an Expression Visitor for the rare cases you don't want to insert every field
435
440
436
441
``` csharp
437
- db .InsertOnly (new Person { FirstName = " Amy" }, q => q .Insert (p => new {p .FirstName }))
442
+ var q = db .From <Person >()
443
+ .Insert (p => new { p .FirstName });
444
+
445
+ db .InsertOnly (new Person { FirstName = " Amy" }, onlyFields : q )
438
446
```
439
447
440
448
### DELETE
@@ -446,7 +454,9 @@ db.Delete<Person>(p => p.Age == 27);
446
454
447
455
Or an Expression Visitor:
448
456
``` csharp
449
- var q = db .From <Person >().Where (p => p .Age == 27 );
457
+ var q = db .From <Person >()
458
+ .Where (p => p .Age == 27 );
459
+
450
460
db .Delete <Person >(q );
451
461
```
452
462
@@ -499,7 +509,7 @@ List<Track> tracks = db.Select<Track>()
499
509
** Single ** returns a single record :
500
510
501
511
```csharp
502
- Track track = db .Single <Track >(q => q .RefId == refId )
512
+ Track track = db .Single <Track >(x => x .RefId == refId )
503
513
```
504
514
505
515
** Dictionary ** returns a Dictionary made from the first two columns :
@@ -590,7 +600,10 @@ Whilst OrmLite aims to provide a light-weight typed wrapper around SQL, it offer
590
600
Starting with the most basic example you can simply specify the table you want to join with :
591
601
592
602
```csharp
593
- var dbCustomers = db .Select <Customer >(q => q .Join <CustomerAddress >());
603
+ var q = db .From <Customer >()
604
+ .Join <CustomerAddress >();
605
+
606
+ var dbCustomers = db .Select <Customer >(q );
594
607
```
595
608
596
609
This query rougly maps to the following SQL:
@@ -721,10 +734,10 @@ var customer = new Customer
721
734
722
735
db .Save (customer , references :true );
723
736
724
- var c = db .LoadSelect <Customer >(q => q .Name == " The Customer" );
737
+ var c = db .LoadSelect <Customer >(x => x .Name == " The Customer" );
725
738
c .WorkAddress .Address .Print (); // 2 Work Road
726
739
727
- var ukAddress = db .Single <CustomerAddress >(q => q .Country == " UK" );
740
+ var ukAddress = db .Single <CustomerAddress >(x => x .Country == " UK" );
728
741
ukAddress .Address .Print (); // 2 Work Road
729
742
```
730
743
@@ -752,7 +765,10 @@ Where `FullCustomerInfo` is any POCO that contains a combination of properties m
752
765
The above example is also equivalent to the shorthand ` db.Select<Into,From>() ` API:
753
766
754
767
``` csharp
755
- var customers = db .Select <FullCustomerInfo ,Customer >(q => q .Join <CustomerAddress >());
768
+ var q = db .From <Customer >()
769
+ .Join <CustomerAddress >();
770
+
771
+ var customers = db .Select <FullCustomerInfo ,Customer >(q );
756
772
```
757
773
758
774
Rules for how results are mapped is simply each property on ` FullCustomerInfo ` is mapped to the first matching property in any of the tables in the order they were added to the SqlExpression.
@@ -861,7 +877,7 @@ var customer = db.LoadSingleById<Customer>(customerId);
861
877
Using Typed SqlExpressions:
862
878
863
879
``` csharp
864
- var customers = db .LoadSelect <Customer >(q => q .Name == " Customer 1" );
880
+ var customers = db .LoadSelect <Customer >(x => x .Name == " Customer 1" );
865
881
```
866
882
867
883
More examples available in [ LoadReferencesTests.cs] ( https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs )
@@ -884,10 +900,12 @@ For example you can select a collection of Customers who've made an order with q
884
900
885
901
``` csharp
886
902
// Select Customers who've had orders with Quantities of 10 or more
887
- List < Customer > customers = db .Select <Customer >(q =>
888
- q .Join <Order >()
889
- .Where <Order >(o => o .Qty >= 10 )
890
- .SelectDistinct ());
903
+ var q = db .From <Customer >()
904
+ .Join <Order >()
905
+ .Where <Order >(o => o .Qty >= 10 )
906
+ .SelectDistinct ();
907
+
908
+ List < Customer > customers = db .Select <Customer >(q );
891
909
892
910
// Select Orders with Quantities of 10 or more
893
911
List < Order > orders = db .Select <Order >(o => o .Qty >= 10 );
@@ -1093,7 +1111,7 @@ using (var db = OpenDbConnection())
1093
1111
db .DropAndCreateTable <PocoTable >();
1094
1112
db .Insert (new PocoTable { Name = " Multiplicity" });
1095
1113
1096
- var rowsInserted = db .Count <PocoTable >(q => q .Name == " Multiplicity" ); // 3
1114
+ var rowsInserted = db .Count <PocoTable >(x => x .Name == " Multiplicity" ); // 3
1097
1115
}
1098
1116
```
1099
1117
@@ -1323,8 +1341,11 @@ For more complex queries you can easily fall back to raw SQL where the Custom SQ
1323
1341
let you to map custom SqlExpressions into different responses:
1324
1342
1325
1343
``` csharp
1326
- List < Person > results = db .SqlList <Person >(
1327
- db .From <Person >().Select (" *" ).Where (q => q .Age < 50 ));
1344
+ var q = db .From <Person >()
1345
+ .Where (x => x .Age < 50 )
1346
+ .Select (" *" );
1347
+ List < Person > results = db .SqlList <Person >(q );
1348
+
1328
1349
List < Person > results = db .SqlList <Person >(
1329
1350
" SELECT * FROM Person WHERE Age < @age" , new { age = 50 });
1330
1351
@@ -1334,8 +1355,10 @@ List<string> results = db.SqlColumn<string>("SELECT LastName FROM Person");
1334
1355
HashSet < int > results = db .ColumnDistinct <int >(db .From <Person >().Select (x => x .Age ));
1335
1356
HashSet < int > results = db .ColumnDistinct <int >(" SELECT Age FROM Person" );
1336
1357
1337
- int result = db .SqlScalar <int >(
1338
- db .From <Person >().Select (Sql .Count (" *" )).Where (q => q .Age < 50 ));
1358
+ var q = db .From <Person >()
1359
+ .Where (x => x .Age < 50 )
1360
+ .Select (Sql .Count (" *" ));
1361
+ int result = db .SqlScalar <int >(q );
1339
1362
int result = db .SqlScalar <int >(" SELCT COUNT(*) FROM Person WHERE Age < 50" );
1340
1363
```
1341
1364
0 commit comments