@@ -12,17 +12,24 @@ public class QueryableResult<T> : GriddlyResult<T>
12
12
{
13
13
IQueryable < T > _result ;
14
14
Func < IQueryable < T > , IQueryable < T > > _massage = null ;
15
+ string _finalSortField ;
15
16
16
- public QueryableResult ( IQueryable < T > result , string viewName = null , Func < IQueryable < T > , IQueryable < T > > massage = null )
17
+ static readonly bool _typeHasId = typeof ( T ) . GetProperty ( "Id" ) != null ;
18
+
19
+ public QueryableResult ( IQueryable < T > result , string viewName = null , Func < IQueryable < T > , IQueryable < T > > massage = null , string finalSortField = null )
17
20
: base ( viewName )
18
21
{
19
22
_result = result ;
20
23
_massage = massage ;
24
+ _finalSortField = finalSortField ;
25
+
26
+ if ( _finalSortField == null && _typeHasId )
27
+ _finalSortField = "Id" ;
21
28
}
22
29
23
30
public override IEnumerable < T > GetAll ( SortField [ ] sortFields )
24
31
{
25
- IQueryable < T > sortedQuery = ApplySortFields ( _result , sortFields ) ;
32
+ IQueryable < T > sortedQuery = ApplySortFields ( _result , sortFields , _finalSortField ) ;
26
33
27
34
if ( _massage != null )
28
35
sortedQuery = _massage ( sortedQuery ) ;
@@ -32,7 +39,7 @@ public override IEnumerable<T> GetAll(SortField[] sortFields)
32
39
33
40
public override IList < T > GetPage ( int pageNumber , int pageSize , SortField [ ] sortFields )
34
41
{
35
- IQueryable < T > sortedQuery = ApplySortFields ( _result , sortFields ) ;
42
+ IQueryable < T > sortedQuery = ApplySortFields ( _result , sortFields , _finalSortField ) ;
36
43
37
44
if ( _massage != null )
38
45
sortedQuery = _massage ( sortedQuery ) ;
@@ -102,7 +109,7 @@ internal void PopulateSummaryValue(GriddlyColumn c)
102
109
103
110
public override IEnumerable < P > GetAllForProperty < P > ( string propertyName , SortField [ ] sortFields )
104
111
{
105
- return ApplySortFields ( _result , sortFields )
112
+ return ApplySortFields ( _result , sortFields , _finalSortField )
106
113
. Select < P > ( propertyName , null ) ;
107
114
}
108
115
@@ -111,7 +118,7 @@ public override long GetCount()
111
118
return _result . Count ( ) ;
112
119
}
113
120
114
- protected static IQueryable < T > ApplySortFields ( IQueryable < T > source , SortField [ ] sortFields )
121
+ protected static IQueryable < T > ApplySortFields ( IQueryable < T > source , SortField [ ] sortFields , string finalSortField )
115
122
{
116
123
IOrderedQueryable < T > sortedQuery = null ;
117
124
@@ -138,6 +145,14 @@ protected static IQueryable<T> ApplySortFields(IQueryable<T> source, SortField[]
138
145
}
139
146
}
140
147
148
+ if ( finalSortField != null )
149
+ {
150
+ if ( sortedQuery == null )
151
+ sortedQuery = OrderByDescending ( source , finalSortField ) ;
152
+ else
153
+ sortedQuery = ThenByDescending ( sortedQuery , finalSortField ) ;
154
+ }
155
+
141
156
return sortedQuery ?? source ;
142
157
}
143
158
0 commit comments