@@ -39,18 +39,13 @@ public SortField[] GetSortFields(NameValueCollection items)
39
39
}
40
40
}
41
41
42
- public class GriddlyResult < T > : GriddlyResult
42
+ public abstract class GriddlyResult < T > : GriddlyResult
43
43
{
44
- IQueryable < T > _result ;
45
- Func < IQueryable < T > , IQueryable < T > > _massage = null ;
46
-
47
44
public string ViewName { get ; set ; }
48
45
49
- public GriddlyResult ( IQueryable < T > result , string viewName = null , Func < IQueryable < T > , IQueryable < T > > massage = null )
46
+ public GriddlyResult ( string viewName = null )
50
47
{
51
- _result = result ;
52
48
ViewName = viewName ;
53
- _massage = massage ;
54
49
}
55
50
56
51
public override void ExecuteResult ( ControllerContext context )
@@ -166,157 +161,13 @@ public override void ExecuteResult(ControllerContext context)
166
161
}
167
162
}
168
163
169
- public virtual IEnumerable < T > GetAll ( SortField [ ] sortFields )
170
- {
171
- IQueryable < T > sortedQuery = ApplySortFields ( _result , sortFields ) ;
172
-
173
- if ( _massage != null )
174
- sortedQuery = _massage ( sortedQuery ) ;
175
-
176
- return sortedQuery ;
177
- }
178
-
179
- public virtual IList < T > GetPage ( int pageNumber , int pageSize , SortField [ ] sortFields )
180
- {
181
- IQueryable < T > sortedQuery = ApplySortFields ( _result , sortFields ) ;
182
-
183
- if ( _massage != null )
184
- sortedQuery = _massage ( sortedQuery ) ;
185
-
186
- return sortedQuery . Skip ( pageNumber * pageSize ) . Take ( pageSize ) . ToList ( ) ;
187
- }
188
-
189
- public virtual void PopulateSummaryValues ( GriddlySettings < T > settings )
190
- {
191
- // Only works for linq to objects
192
- //List<GriddlyColumn> summaryColumns = settings.Columns.Where(x => x.SummaryFunction != null).ToList();
193
-
194
- //if (summaryColumns.Any())
195
- //{
196
- // StringBuilder aggregateExpression = new StringBuilder();
197
-
198
- // aggregateExpression.Append("new (");
199
-
200
- // for (int i = 0; i < summaryColumns.Count; i++)
201
- // {
202
- // if (i > 0)
203
- // aggregateExpression.Append(", ");
164
+ public abstract IEnumerable < T > GetAll ( SortField [ ] sortFields ) ;
165
+
166
+ public abstract IList < T > GetPage ( int pageNumber , int pageSize , SortField [ ] sortFields ) ;
167
+
168
+ public abstract void PopulateSummaryValues ( GriddlySettings < T > settings ) ;
204
169
205
- // GriddlyColumn col = summaryColumns[i];
206
-
207
- // aggregateExpression.AppendFormat("{0}({1}) AS _a{2}", col.SummaryFunction, col.ExpressionString, i);
208
- // }
209
-
210
- // aggregateExpression.Append(")");
211
-
212
- // var query = _result.GroupBy(x => 1).Select(aggregateExpression.ToString());
213
- // var item = query.Cast<object>().Single();
214
- // var type = item.GetType();
215
-
216
- // for (int i = 0; i < summaryColumns.Count; i++)
217
- // summaryColumns[i].SummaryValue = type.GetProperty("_a" + i).GetValue(item);
218
- //}
219
-
220
- // TODO: figure out how to get this in one query
221
- foreach ( GriddlyColumn c in settings . Columns . Where ( x => x . SummaryFunction != null ) )
222
- {
223
- switch ( c . SummaryFunction . Value )
224
- {
225
- case SummaryAggregateFunction . Sum :
226
- case SummaryAggregateFunction . Average :
227
- case SummaryAggregateFunction . Min :
228
- case SummaryAggregateFunction . Max :
229
- c . SummaryValue = _result . Aggregate ( c . SummaryFunction . Value . ToString ( ) , c . ExpressionString ) ;
230
-
231
- break ;
232
-
233
- default :
234
- throw new InvalidOperationException ( string . Format ( "Unknown summary function {0} for column {1}." , c . SummaryFunction , c . ExpressionString ) ) ;
235
- }
236
- }
237
- }
238
-
239
- public virtual long GetCount ( )
240
- {
241
- return _result . Count ( ) ;
242
- }
243
-
244
- protected static IQueryable < T > ApplySortFields ( IQueryable < T > source , SortField [ ] sortFields )
245
- {
246
- IOrderedQueryable < T > sortedQuery = null ;
247
-
248
- if ( sortFields != null )
249
- {
250
- for ( int i = 0 ; i < sortFields . Length ; i ++ )
251
- {
252
- SortField sortField = sortFields [ i ] ;
253
-
254
- if ( sortField . Direction == SortDirection . Ascending )
255
- {
256
- if ( i == 0 )
257
- sortedQuery = OrderBy ( source , sortField . Field ) ;
258
- else
259
- sortedQuery = ThenBy ( sortedQuery , sortField . Field ) ;
260
- }
261
- else
262
- {
263
- if ( i == 0 )
264
- sortedQuery = OrderByDescending ( source , sortField . Field ) ;
265
- else
266
- sortedQuery = ThenByDescending ( sortedQuery , sortField . Field ) ;
267
- }
268
- }
269
- }
270
-
271
- return sortedQuery ?? source ;
272
- }
273
-
274
- // http://stackoverflow.com/a/233505/8037
275
- static IOrderedQueryable < T > OrderBy ( IQueryable < T > source , string property )
276
- {
277
- return ApplyOrder ( source , property , "OrderBy" ) ;
278
- }
279
-
280
- static IOrderedQueryable < T > OrderByDescending ( IQueryable < T > source , string property )
281
- {
282
- return ApplyOrder ( source , property , "OrderByDescending" ) ;
283
- }
284
-
285
- static IOrderedQueryable < T > ThenBy ( IOrderedQueryable < T > source , string property )
286
- {
287
- return ApplyOrder ( source , property , "ThenBy" ) ;
288
- }
289
-
290
- static IOrderedQueryable < T > ThenByDescending ( IOrderedQueryable < T > source , string property )
291
- {
292
- return ApplyOrder ( source , property , "ThenByDescending" ) ;
293
- }
294
-
295
- static IOrderedQueryable < T > ApplyOrder ( IQueryable < T > source , string property , string methodName )
296
- {
297
- string [ ] props = property . Split ( '.' ) ;
298
- Type type = typeof ( T ) ;
299
- ParameterExpression arg = Expression . Parameter ( type , "x" ) ;
300
- Expression expr = arg ;
301
- foreach ( string prop in props )
302
- {
303
- // use reflection (not ComponentModel) to mirror LINQ
304
- PropertyInfo pi = type . GetProperty ( prop ) ;
305
- expr = Expression . Property ( expr , pi ) ;
306
- type = pi . PropertyType ;
307
- }
308
- Type delegateType = typeof ( Func < , > ) . MakeGenericType ( typeof ( T ) , type ) ;
309
- LambdaExpression lambda = Expression . Lambda ( delegateType , expr , arg ) ;
310
-
311
- object result = typeof ( Queryable ) . GetMethods ( ) . Single (
312
- method => method . Name == methodName
313
- && method . IsGenericMethodDefinition
314
- && method . GetGenericArguments ( ) . Length == 2
315
- && method . GetParameters ( ) . Length == 2 )
316
- . MakeGenericMethod ( typeof ( T ) , type )
317
- . Invoke ( null , new object [ ] { source , lambda } ) ;
318
- return ( IOrderedQueryable < T > ) result ;
319
- }
170
+ public abstract long GetCount ( ) ;
320
171
}
321
172
322
173
public enum GriddlyExportFormat
0 commit comments