You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dynamic-linq.net/pages/getting-started/advanced/expression-language.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ The expression language defines the following constants:
20
20
## Identifiers
21
21
22
22
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`.
24
24
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:
Copy file name to clipboardExpand all lines: dynamic-linq.net/pages/getting-started/basic/simple-query.md
+63-41
Original file line number
Diff line number
Diff line change
@@ -12,17 +12,17 @@ When querying data using Dynamic LINQ, everything can be expressed using strings
12
12
Strongly typed LINQ looks like:
13
13
14
14
```csharp
15
-
1:varresult=context.Customers
16
-
2: .Where(c=>c.City=="Paris")
17
-
3: .ToList();
15
+
varresult=context.Customers
16
+
.Where(c=>c.City=="Paris")
17
+
.ToList();
18
18
```
19
19
20
20
Dynamic LINQ looks like:
21
21
22
22
```csharp
23
-
1:varresultDynamic=context.Customers
24
-
2: .Where("City == \"Paris\"")
25
-
3: .ToList();
23
+
varresultDynamic=context.Customers
24
+
.Where("City == \"Paris\"")
25
+
.ToList();
26
26
```
27
27
28
28
When we break down these examples:
@@ -51,10 +51,10 @@ Note that instead of using the query value directly in the string like `"City ==
51
51
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:
52
52
53
53
```csharp
54
-
1:stringcityToSearch="Paris";
55
-
2:varresultDynamic=context.Customers
56
-
3: .Where("City == @0", cityToSearch)
57
-
4: .ToList();
54
+
stringcityToSearch="Paris";
55
+
varresultDynamic=context.Customers
56
+
.Where("City == @0", cityToSearch)
57
+
.ToList();
58
58
```
59
59
60
60
[View all above examples here](https://dotnetfiddle.net/cs6MRX)
@@ -66,33 +66,33 @@ See chapters below for more basic query examples.
66
66
Note that it's also possible to query data using multiple predicates:
67
67
68
68
```csharp
69
-
1:varresult=context.Customers
70
-
2: .Where(c=>c.City=="Paris"&&c.Age>50)
71
-
3: .ToList();
69
+
varresult=context.Customers
70
+
.Where(c=>c.City=="Paris"&&c.Age>50)
71
+
.ToList();
72
72
```
73
73
74
74
This code can be used when using Dynamic LINQ:
75
75
76
76
```csharp
77
-
1:varresultDynamic=context.Customers
78
-
2: .Where("City == \"Paris\" && Age > 50")
79
-
3: .ToList();
77
+
varresultDynamic=context.Customers
78
+
.Where("City == \"Paris\" && Age > 50")
79
+
.ToList();
80
80
```
81
81
82
82
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:
83
83
84
84
```csharp
85
-
1:varresultDynamic=context.Customers
86
-
2: .Where("City == @0 and Age > @1", "Paris", 50)
87
-
3: .ToList();
85
+
varresultDynamic=context.Customers
86
+
.Where("City == @0 and Age > @1", "Paris", 50)
87
+
.ToList();
88
88
```
89
89
90
90
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:
[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
104
104
Here is an example of a strongly typed:
105
105
106
106
```csharp
107
-
1:varresult=context.Customers
108
-
2: .Select(c=>new { c.City, c.CompanyName } )
109
-
3: .ToList();
107
+
varresult=context.Customers
108
+
.Select(c=>new { c.City, c.CompanyName } )
109
+
.ToList();
110
110
```
111
111
112
112
Dynamic LINQ looks like:
113
113
114
114
```csharp
115
-
1:varresultDynamic=context.Customers
116
-
2: .Select("new { City, CompanyName }")
117
-
3: .ToDynamicList();
115
+
varresultDynamic=context.Customers
116
+
.Select("new { City, CompanyName }")
117
+
.ToDynamicList();
118
118
```
119
119
120
120
When we break down these two examples:
@@ -132,17 +132,17 @@ Sorting the result based on a property or multiple properties is another feature
132
132
A strongly typed example to sort on City and then on CompanyName would look like:
133
133
134
134
```csharp
135
-
1:varordered=context.Customers
136
-
2: .OrderBy(c=>c.City).ThenBy(c=>c.CompanyName)
137
-
3: .ToList();
135
+
varordered=context.Customers
136
+
.OrderBy(c=>c.City).ThenBy(c=>c.CompanyName)
137
+
.ToList();
138
138
```
139
139
140
140
Dynamic LINQ looks like:
141
141
142
142
```csharp
143
-
1:varorderedDynamic=context.Customers
144
-
2: .OrderBy("City, CompanyName")
145
-
3: .ToList();
143
+
varorderedDynamic=context.Customers
144
+
.OrderBy("City, CompanyName")
145
+
.ToList();
146
146
```
147
147
148
148
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
157
157
In case you want to sort Descending, or a mixed combination, you can use the following Dynamic LINQ:
158
158
159
159
```csharp
160
-
1:varorderedDynamic=context.Customers
161
-
2: .OrderBy("City").ThenBy("CompanyName desc")
162
-
3: .ToList();
160
+
varorderedDynamic=context.Customers
161
+
.OrderBy("City").ThenBy("CompanyName desc")
162
+
.ToList();
163
163
```
164
164
165
165
Or
166
166
167
167
```csharp
168
-
1:varorderedDynamic=context.Customers
169
-
2: .OrderBy("City, CompanyName desc")
170
-
3: .ToList();
168
+
varorderedDynamic=context.Customers
169
+
.OrderBy("City, CompanyName desc")
170
+
.ToList();
171
+
```
172
+
173
+
It's also possible to use `ThenBy`:
174
+
175
+
```csharp
176
+
varorderedDynamic=context.Customers
177
+
.OrderBy("City").ThenBy("CompanyName desc")
178
+
.ToList();
171
179
```
172
180
173
181
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.
174
182
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:
0 commit comments