Skip to content

Commit 1e74861

Browse files
committed
Update docs : ordering
1 parent c246816 commit 1e74861

File tree

2 files changed

+65
-43
lines changed

2 files changed

+65
-43
lines changed

dynamic-linq.net/pages/getting-started/advanced/expression-language.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ The expression language defines the following constants:
2020
## Identifiers
2121

2222
An Identifier consists of a letter or underscore followed by any number of letters, digits, or underscores. In order to reference an identifier with the same spelling as a keyword, the identifier must be prefixed with a single `@` character.
23-
Some examples of identifiers: `x`, `Hello`, `m_1`, `@true` and `@String`
23+
Some examples of identifiers: `x`, `Hello`, `m_1`, `@true` and `@String`.
2424

25-
Identifiers of the from `@x`, where x is an integral number greater than or equal to zero, are used to denote the substitution values, if any, that were passed to the expression parser. For example:
25+
Identifiers of the form `@x`, where x is an integral number greater than or equal to zero, are used to denote the substitution values, if any, that were passed to the expression parser. For example:
2626

2727
```csharp
2828
customers.Where("Country = @0", country);

dynamic-linq.net/pages/getting-started/basic/simple-query.md

+63-41
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ When querying data using Dynamic LINQ, everything can be expressed using strings
1212
Strongly typed LINQ looks like:
1313

1414
```csharp
15-
1: var result = context.Customers
16-
2: .Where(c => c.City == "Paris")
17-
3: .ToList();
15+
var result = context.Customers
16+
.Where(c => c.City == "Paris")
17+
.ToList();
1818
```
1919

2020
Dynamic LINQ looks like:
2121

2222
```csharp
23-
1: var resultDynamic = context.Customers
24-
2: .Where("City == \"Paris\"")
25-
3: .ToList();
23+
var resultDynamic = context.Customers
24+
.Where("City == \"Paris\"")
25+
.ToList();
2626
```
2727

2828
When we break down these examples:
@@ -51,10 +51,10 @@ Note that instead of using the query value directly in the string like `"City ==
5151
And of course, it's not required to specify the value as a hard-coded value. So another valid Dynamic LINQ query example will be:
5252

5353
```csharp
54-
1: string cityToSearch = "Paris";
55-
2: var resultDynamic = context.Customers
56-
3: .Where("City == @0", cityToSearch)
57-
4: .ToList();
54+
string cityToSearch = "Paris";
55+
var resultDynamic = context.Customers
56+
.Where("City == @0", cityToSearch)
57+
.ToList();
5858
```
5959

6060
[View all above examples here](https://dotnetfiddle.net/cs6MRX)
@@ -66,33 +66,33 @@ See chapters below for more basic query examples.
6666
Note that it's also possible to query data using multiple predicates:
6767

6868
```csharp
69-
1: var result = context.Customers
70-
2: .Where(c => c.City == "Paris" && c.Age > 50)
71-
3: .ToList();
69+
var result = context.Customers
70+
.Where(c => c.City == "Paris" && c.Age > 50)
71+
.ToList();
7272
```
7373

7474
This code can be used when using Dynamic LINQ:
7575

7676
```csharp
77-
1: var resultDynamic = context.Customers
78-
2: .Where("City == \"Paris\" && Age > 50")
79-
3: .ToList();
77+
var resultDynamic = context.Customers
78+
.Where("City == \"Paris\" && Age > 50")
79+
.ToList();
8080
```
8181

8282
Note that instead of the `&&`-operand, you can use `and`. Also in this example you can use composite string formatting. In that case, the Dynamic LINQ will be:
8383

8484
```csharp
85-
1: var resultDynamic = context.Customers
86-
2: .Where("City == @0 and Age > @1", "Paris", 50)
87-
3: .ToList();
85+
var resultDynamic = context.Customers
86+
.Where("City == @0 and Age > @1", "Paris", 50)
87+
.ToList();
8888
```
8989

9090
You can also mimic the strongly typed `Where`-predicate using the [=> operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-operator), like this:
9191

9292
```csharp
93-
1: var resultDynamic = context.Customers
94-
2: .Where("c => c.City == \"Paris\" && c.Age > 50")
95-
3: .ToList();
93+
var resultDynamic = context.Customers
94+
.Where("c => c.City == \"Paris\" && c.Age > 50")
95+
.ToList();
9696
```
9797

9898
[View all above examples here](https://dotnetfiddle.net/4yOUhM)
@@ -104,17 +104,17 @@ Selecting entities or only the properties you need is another powerful feature o
104104
Here is an example of a strongly typed:
105105

106106
```csharp
107-
1: var result = context.Customers
108-
2: .Select(c => new { c.City, c.CompanyName } )
109-
3: .ToList();
107+
var result = context.Customers
108+
.Select(c => new { c.City, c.CompanyName } )
109+
.ToList();
110110
```
111111

112112
Dynamic LINQ looks like:
113113

114114
```csharp
115-
1: var resultDynamic = context.Customers
116-
2: .Select("new { City, CompanyName }")
117-
3: .ToDynamicList();
115+
var resultDynamic = context.Customers
116+
.Select("new { City, CompanyName }")
117+
.ToDynamicList();
118118
```
119119

120120
When we break down these two examples:
@@ -132,17 +132,17 @@ Sorting the result based on a property or multiple properties is another feature
132132
A strongly typed example to sort on City and then on CompanyName would look like:
133133

134134
```csharp
135-
1: var ordered = context.Customers
136-
2: .OrderBy(c => c.City).ThenBy(c => c.CompanyName)
137-
3: .ToList();
135+
var ordered = context.Customers
136+
.OrderBy(c => c.City).ThenBy(c => c.CompanyName)
137+
.ToList();
138138
```
139139

140140
Dynamic LINQ looks like:
141141

142142
```csharp
143-
1: var orderedDynamic = context.Customers
144-
2: .OrderBy("City, CompanyName")
145-
3: .ToList();
143+
var orderedDynamic = context.Customers
144+
.OrderBy("City, CompanyName")
145+
.ToList();
146146
```
147147

148148
When we break down these two examples:
@@ -157,19 +157,41 @@ For Dynamic Linq you only need to use the `OrderBy`-method, and provide the two
157157
In case you want to sort Descending, or a mixed combination, you can use the following Dynamic LINQ:
158158

159159
```csharp
160-
1: var orderedDynamic = context.Customers
161-
2: .OrderBy("City").ThenBy("CompanyName desc")
162-
3: .ToList();
160+
var orderedDynamic = context.Customers
161+
.OrderBy("City").ThenBy("CompanyName desc")
162+
.ToList();
163163
```
164164

165165
Or
166166

167167
```csharp
168-
1: var orderedDynamic = context.Customers
169-
2: .OrderBy("City, CompanyName desc")
170-
3: .ToList();
168+
var orderedDynamic = context.Customers
169+
.OrderBy("City, CompanyName desc")
170+
.ToList();
171+
```
172+
173+
It's also possible to use `ThenBy`:
174+
175+
```csharp
176+
var orderedDynamic = context.Customers
177+
.OrderBy("City").ThenBy("CompanyName desc")
178+
.ToList();
171179
```
172180

173181
The examples above will perform the sorting on the City property *Ascending* (= default) and then the sorting will be on the CompanyName in a *Descending* way.
174182

175-
[View all above examples here](https://dotnetfiddle.net/GdxsMG)
183+
Note that it's *not* possible to use identifiers of the form `@x` to define the property name or the sorting direction.
184+
The identifiers `@x` are only used for the (constant) values in the query.
185+
186+
[View all above examples here](https://dotnetfiddle.net/GdxsMG)
187+
188+
189+
### Providing a comparer
190+
It's also possible to provide a comparer to the `OrderBy` or `ThenBy`. This can be done by providing class which implements the `IComparer<T>` interface.
191+
192+
An example of provising a comparer for a string property:
193+
```csharp
194+
var result = context.Customers
195+
.OrderBy("City", StringComparer.OrdinalIgnoreCase)
196+
.ToList();
197+
```

0 commit comments

Comments
 (0)