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: README.md
+109-2
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,8 @@ The parser aims to be Database and Provider agnostic. It currently targets Netst
15
15
* Sql Server
16
16
* PostgreSQL
17
17
18
+
If you intend to filter your dataset, the default configuration assumes your IQueryable uses a provider with support for .ToString() on DateTime and numeric types. Please note that EFCore will not fail but instead fall back to client evaluation if this requirement is not met. I have mixed feelings about this. I believe client evaluation should be opt-in and not opt-out.
19
+
18
20
jQuery Datatables
19
21
========================
20
22
@@ -114,7 +116,112 @@ The following snippets were taken from the aspnet-core-sample project also locat
114
116
115
117
The included Dockerfile-websample builds, packages and runs the web sample project in a docker image. No tools, frameworks or runtimes are required on the host machine. The image has been published to docker for your convenience.
116
118
117
-
docker run -p 80:80 garvincasimir/datatables-aspnet-core-sample:0.0.2
119
+
docker run -p 80:80 garvincasimir/datatables-aspnet-core-sample:0.0.2
120
+
121
+
Custom Filter Expressions
122
+
========================
123
+
The parser builds a set of expressions based on the settings and filter text sent from Datatables. The end result is a *WHERE* clause which looks something like this:
124
+
125
+
```
126
+
FROM [People] AS [val]
127
+
WHERE ((((CASE
128
+
WHEN CHARINDEX(N'cromie', LOWER([val].[FirstName])) > 0
129
+
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
130
+
END | CASE
131
+
WHEN CHARINDEX(N'cromie', LOWER([val].[LastName])) > 0
132
+
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
133
+
END) | CASE
134
+
WHEN CHARINDEX(N'cromie', LOWER(CONVERT(VARCHAR(100), [val].[BirthDate]))) > 0
135
+
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
136
+
END) | CASE
137
+
WHEN CHARINDEX(N'cromie', LOWER(CONVERT(VARCHAR(100), [val].[Weight]))) > 0
138
+
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
139
+
END) | CASE
140
+
WHEN CHARINDEX(N'cromie', LOWER(CONVERT(VARCHAR(11), [val].[Children]))) > 0
141
+
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
142
+
END) = 1
143
+
```
144
+
145
+
In the above example, each of the case statements will attempt to find the filter text 'cromie' within a string representation of the properties from *T*. The exact mechanics and syntax will vary by provider and db engine but this is an example of the actual query sent to your backend database.
146
+
147
+
The expression generated by the parser looks like this:
148
+
149
+
```
150
+
where val.FirstName.ToLower().Contains("cromie") || val.LastName.ToLower().Contains("cromie") || val.BirthDate.ToString().ToLower().Contains("cromie") || val.Weight.ToString().ToLower().Contains("cromie") || val.Children.ToString().ToLower().Contains("cromie")
151
+
```
152
+
153
+
What is missing in the above expression is the ability to format dates to match the client side. So it may seem strange to a user if they enter a date in the filter text box and no results are returned. It would be nice if providers just supported *DateTime.ToString(string format)* right? Even if they did, the format strings expected by db engines are not consistent at all. As a result, I decided to expose some of the internals of the parser and allow library users to substitute .ToString() with a custom expression.
154
+
155
+
For example, if your provider does support *DateTime.ToString(string format)*, you can substitute .ToString() with that expression after initializing the parser. This must be explicitly called for each applicable property.
156
+
157
+
```
158
+
var parser = new Parser<Person>(p, context.People)
159
+
.SetConverter(x => x.BirthDate, x => x.BirthDate.ToString("M/dd/yyyy"))
160
+
.SetConverter(x => x.LastUpdated, x => x.LastUpdated.ToString("M/dd/yyyy"));
161
+
```
162
+
163
+
**EF Core 2**
164
+
165
+
In EF Core 2 you can map user defined and system scalar valued functions and use them for formatting. The following is an example for SQL Server >= 2012.
166
+
167
+
PersonContext.cs
168
+
```
169
+
using Microsoft.EntityFrameworkCore;
170
+
using System;
171
+
172
+
namespace DataTablesParser.Tests
173
+
{
174
+
public class PersonContext : DbContext
175
+
{
176
+
public PersonContext(){ }
177
+
178
+
public PersonContext(DbContextOptions<PersonContext> options)
0 commit comments