Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 2475608

Browse files
committed
Add more API examples
1 parent a7fb238 commit 2475608

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ int result = db.SqlScalar<int>("SELECT 10");
8787

8888
Some more examples can be found in [SqlServerProviderTests](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/SqlServerProviderTests.cs).
8989

90+
### Using typed SqlExpression in Custom SQL APIs
91+
92+
From v4.0.16 you can now use typed sql expressions in Custom SQL API's
93+
94+
```csharp
95+
List<Person> results = db.SqlList<Person>(db.From<Person>().Select("*").Where(q => q.Age < 50));
96+
List<string> results = db.SqlColumn<string>(db.From<Person>().Select(x => x.LastName).Where(q => q.Age < 50));
97+
HashSet<int> results = db.ColumnDistinct<int>(db.From<Person>().Select(x => x.Age).Where(q => q.Age < 50));
98+
int result = db.SqlScalar<int>(db.From<Person>().Select(Sql.Count("*")).Where(q => q.Age < 50));
99+
```
100+
90101
## New Simplified API
91102
We've streamlined our API, now all OrmLite extensions that used to be on `IDbCommand` now hang off `IDbConnection`
92103
(just like Dapper), this reduces the boiler-plate when opening a connection to a single line, so now you can
@@ -268,6 +279,71 @@ db.Select<Author>(q => q.Rate == 10 && q.City == "Mexico");
268279
Right now the Expression support can satisfy most simple queries with a strong-typed API.
269280
For anything more complex (e.g. queries with table joins) you can still easily fall back to raw SQL queries as seen below.
270281

282+
### Convenient common usage data access patterns
283+
284+
OrmLite also includes a number of convenient API's providing DRY, typed data access for common queries:
285+
286+
```csharp
287+
Person personById = db.SingleById<Person>(1);
288+
```
289+
290+
**SELECT "Id", "FirstName", "LastName", "Age" FROM "Person" WHERE "Id" = @Id**
291+
292+
```csharp
293+
Person personByAge = db.Single<Person>(x => x.Age == 42);
294+
```
295+
296+
**SELECT TOP 1 "Id", "FirstName", "LastName", "Age" FROM "Person" WHERE ("Age" = 42)**
297+
298+
```csharp
299+
int maxAgeUnder50 = db.Scalar<Person, int>(x => Sql.Max(x.Age), x => x.Age < 50);
300+
```
301+
302+
**SELECT Max("Age") FROM "Person" WHERE ("Age" < 50)**
303+
304+
```csharp
305+
int peopleOver40 = db.Scalar<int>(db.From<Person>().Select(Sql.Count("*")).Where(q => q.Age > 40));
306+
```
307+
308+
**SELECT COUNT(*) FROM "Person" WHERE ("Age" > 40)**
309+
310+
```csharp
311+
int peopleUnder50 = db.Count<Person>(x => x.Age < 50);
312+
```
313+
314+
**SELECT COUNT(*) FROM "Person" WHERE ("Age" < 50)**
315+
316+
```csharp
317+
bool has42YearOlds = db.Exists<Person>(new { Age = 42 });
318+
```
319+
320+
**WHERE "Age" = @Age**
321+
322+
```csharp
323+
List<string> results = db.Column<string>(db.From<Person>().Select(x => x.LastName).Where(q => q.Age == 27));
324+
```
325+
326+
**SELECT "LastName" FROM "Person" WHERE ("Age" = 27)**
327+
328+
```csharp
329+
HashSet<int> results = db.ColumnDistinct<int>(db.From<Person>().Select(x => x.Age).Where(q => q.Age < 50));
330+
```
331+
332+
**SELECT "Age" FROM "Person" WHERE ("Age" < 50)**
333+
334+
```csharp
335+
Dictionary<int,string> results = db.Dictionary<int, string>(db.From<Person>().Select(x => new { x.Id, x.LastName }).Where(x => x.Age < 50));
336+
```
337+
338+
**SELECT "Id","LastName" FROM "Person" WHERE ("Age" < 50)**
339+
340+
341+
```csharp
342+
Dictionary<int, List<string>> results = db.Lookup<int, string>(db.From<Person>().Select(x => new { x.Age, x.LastName }).Where(q => q.Age < 50));
343+
```
344+
345+
**SELECT "Age","LastName" FROM "Person" WHERE ("Age" < 50)**
346+
271347
### INSERT, UPDATE and DELETEs
272348

273349
To see the behaviour of the different APIs, all examples uses this simple model

0 commit comments

Comments
 (0)