@@ -12,17 +12,24 @@ public class QueryableResult<T> : GriddlyResult<T>
1212    { 
1313        IQueryable < T >  _result ; 
1414        Func < IQueryable < T > ,  IQueryable < T > >  _massage  =  null ; 
15+         string  _finalSortField ; 
1516
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 ) 
1720            :  base ( viewName ) 
1821        { 
1922            _result  =  result ; 
2023            _massage  =  massage ; 
24+             _finalSortField  =  finalSortField ; 
25+ 
26+             if  ( _finalSortField  ==  null  &&  _typeHasId ) 
27+                 _finalSortField  =  "Id" ; 
2128        } 
2229
2330        public  override  IEnumerable < T >  GetAll ( SortField [ ]  sortFields ) 
2431        { 
25-             IQueryable < T >  sortedQuery  =  ApplySortFields ( _result ,  sortFields ) ; 
32+             IQueryable < T >  sortedQuery  =  ApplySortFields ( _result ,  sortFields ,   _finalSortField ) ; 
2633
2734            if  ( _massage  !=  null ) 
2835                sortedQuery  =  _massage ( sortedQuery ) ; 
@@ -32,7 +39,7 @@ public override IEnumerable<T> GetAll(SortField[] sortFields)
3239
3340        public  override  IList < T >  GetPage ( int  pageNumber ,  int  pageSize ,  SortField [ ]  sortFields ) 
3441        { 
35-             IQueryable < T >  sortedQuery  =  ApplySortFields ( _result ,  sortFields ) ; 
42+             IQueryable < T >  sortedQuery  =  ApplySortFields ( _result ,  sortFields ,   _finalSortField ) ; 
3643
3744            if  ( _massage  !=  null ) 
3845                sortedQuery  =  _massage ( sortedQuery ) ; 
@@ -102,7 +109,7 @@ internal void PopulateSummaryValue(GriddlyColumn c)
102109
103110        public  override  IEnumerable < P >  GetAllForProperty < P > ( string  propertyName ,  SortField [ ]  sortFields ) 
104111        { 
105-             return  ApplySortFields ( _result ,  sortFields ) 
112+             return  ApplySortFields ( _result ,  sortFields ,   _finalSortField ) 
106113                . Select < P > ( propertyName ,  null ) ; 
107114        } 
108115
@@ -111,7 +118,7 @@ public override long GetCount()
111118            return  _result . Count ( ) ; 
112119        } 
113120
114-         protected  static IQueryable < T >  ApplySortFields ( IQueryable < T >  source ,  SortField [ ]  sortFields ) 
121+         protected  static IQueryable < T >  ApplySortFields ( IQueryable < T >  source ,  SortField [ ]  sortFields ,   string   finalSortField ) 
115122        { 
116123            IOrderedQueryable < T >  sortedQuery  =  null ; 
117124
@@ -138,6 +145,14 @@ protected static IQueryable<T> ApplySortFields(IQueryable<T> source, SortField[]
138145                } 
139146            } 
140147
148+             if  ( finalSortField  !=  null ) 
149+             { 
150+                 if  ( sortedQuery  ==  null ) 
151+                     sortedQuery  =  OrderByDescending ( source ,  finalSortField ) ; 
152+                 else 
153+                     sortedQuery  =  ThenByDescending ( sortedQuery ,  finalSortField ) ; 
154+             } 
155+ 
141156            return  sortedQuery  ??  source ; 
142157        } 
143158
0 commit comments