@@ -10,6 +10,7 @@ namespace DataTablesParser
10
10
{
11
11
public class Parser < T > where T : class
12
12
{
13
+ private IQueryable < T > _originalQuery ;
13
14
private IQueryable < T > _query ;
14
15
private readonly Dictionary < string , string > _config ;
15
16
private readonly Type _type ;
@@ -42,6 +43,7 @@ public class Parser<T> where T : class
42
43
43
44
public Parser ( IEnumerable < KeyValuePair < string , StringValues > > configParams , IQueryable < T > query )
44
45
{
46
+ _originalQuery = query ;
45
47
_query = query ;
46
48
_config = configParams . ToDictionary ( k => k . Key , v=> v . Value . First ( ) . Trim ( ) ) ;
47
49
_type = typeof ( T ) ;
@@ -104,7 +106,7 @@ public Results<T> Parse()
104
106
list . draw = int . Parse ( _config [ Constants . DRAW ] ) ;
105
107
106
108
// count the record BEFORE filtering
107
- list . recordsTotal = _query . Count ( ) ;
109
+ list . recordsTotal = _originalQuery . Count ( ) ;
108
110
109
111
//sort results if sorting isn't disabled or skip needs to be called
110
112
if ( ! _sortDisabled || _skip > 0 )
@@ -124,16 +126,19 @@ public Results<T> Parse()
124
126
resultQuery = _query . Where ( entityFilter )
125
127
. Skip ( _skip )
126
128
. Take ( _take ) ;
127
-
128
- list . recordsFiltered = _query . Count ( entityFilter ) ;
129
+
130
+ list . recordsFiltered = _query . Count ( entityFilter ) ;
129
131
}
130
132
else
131
133
{
132
134
resultQuery = _query
133
135
. Skip ( _skip )
134
136
. Take ( _take ) ;
135
-
136
- list . recordsFiltered = list . recordsTotal ;
137
+
138
+ if ( _query == _originalQuery )
139
+ list . recordsFiltered = list . recordsTotal ;
140
+ else
141
+ list . recordsFiltered = _query . Count ( ) ;
137
142
138
143
}
139
144
@@ -191,6 +196,17 @@ public Parser<T> SetEndsWithToken(string token)
191
196
return this ;
192
197
}
193
198
199
+ /// <summary>
200
+ /// AddCustomFilter add external custom filter to handle complex filtering logic
201
+ /// For example date range filtering
202
+ /// </summary>
203
+ /// <param name="predicate">Filter logic</param>
204
+ /// <returns></returns>
205
+ public Parser < T > AddCustomFilter ( Expression < Func < T , bool > > predicate )
206
+ {
207
+ _query = _query . Where ( predicate ) ;
208
+ return this ;
209
+ }
194
210
195
211
private void ApplySort ( )
196
212
{
@@ -359,7 +375,7 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
359
375
360
376
if ( globalFilterConst != null )
361
377
{
362
- var globalTest = Expression . Call ( toLower , typeof ( string ) . GetMethod ( globalFilterFn , new [ ] { typeof ( string ) } ) , globalFilterConst ) ;
378
+ Expression globalTest = Expression . Call ( toLower , typeof ( string ) . GetMethod ( globalFilterFn , new [ ] { typeof ( string ) } ) , globalFilterConst ) ;
363
379
364
380
if ( filterExpr == null )
365
381
{
0 commit comments