@@ -31,7 +31,7 @@ public SortingOptions(Expression<Func<T, TSortKey>> sortExpression, bool isDesce
3131 _primarySortAction = q => q . OrderBy ( sortExpression ) ;
3232 }
3333
34- _primarySortToString = String . Format ( "{0}-{1}" , sortExpression , isDescending ) ;
34+ _primarySortToString = string . Format ( "{0}-{1}" , sortExpression , isDescending ) ;
3535 }
3636
3737 public void ThenSortBy < TNewSortKey > ( Expression < Func < T , TNewSortKey > > sortExpression , bool isDescending = false )
@@ -48,7 +48,7 @@ public void ThenSortBy<TNewSortKey>(Expression<Func<T, TNewSortKey>> sortExpress
4848 }
4949
5050 _sortActions . Add ( sortAction ) ;
51- _sortActionsToString . Add ( String . Format ( "{0}-{1}" , sortExpression , isDescending ) ) ;
51+ _sortActionsToString . Add ( string . Format ( "{0}-{1}" , sortExpression , isDescending ) ) ;
5252 }
5353
5454
@@ -75,11 +75,11 @@ public virtual IQueryable<T> Apply(IQueryable<T> query)
7575 /// <returns>Unique key for a query</returns>
7676 public override string ToString ( )
7777 {
78- return String . Format ( "SortingOptions<{0},{1}>\n Sort: {2}\n Extra: {3}" ,
79- ( typeof ( T ) ) . Name ,
80- ( typeof ( TSortKey ) ) . Name ,
81- _primarySortToString ?? "null" ,
82- String . Join ( "-" , _sortActionsToString )
78+ return string . Format ( "SortingOptions<{0},{1}>\n Sort: {2}\n Extra: {3}" ,
79+ typeof ( T ) . Name ,
80+ typeof ( TSortKey ) . Name ,
81+ _primarySortToString ?? "null" ,
82+ string . Join ( "-" , _sortActionsToString )
8383 ) ;
8484 }
8585 }
@@ -108,7 +108,7 @@ public SortingOptions(string sortProperty, bool isDescending = false)
108108 _primarySortAction = q => q . OrderByProperty ( sortProperty ) ;
109109 }
110110
111- _primarySortToString = String . Format ( "{0}-{1}" , sortProperty , isDescending ) ;
111+ _primarySortToString = string . Format ( "{0}-{1}" , sortProperty , isDescending ) ;
112112 }
113113
114114 public void ThenSortBy ( string sortProperty , bool isDescending = false )
@@ -125,7 +125,7 @@ public void ThenSortBy(string sortProperty, bool isDescending = false)
125125 }
126126
127127 _sortActions . Add ( sortAction ) ;
128- _sortActionsToString . Add ( String . Format ( "{0}-{1}" , sortProperty , isDescending ) ) ;
128+ _sortActionsToString . Add ( string . Format ( "{0}-{1}" , sortProperty , isDescending ) ) ;
129129 }
130130
131131 /// <summary>
@@ -147,16 +147,35 @@ public virtual IQueryable<T> Apply(IQueryable<T> query)
147147 return _sortActions . Aggregate ( sortedQuery , ( current , sortAction ) => sortAction ( current ) ) ;
148148 }
149149
150+ /// <summary>
151+ /// Applies sorting to the specified query.
152+ /// </summary>
153+ /// <param name="query">The query.</param>
154+ /// <returns>Sorted results.</returns>
155+ public virtual IQueryable < TResult > Apply < TResult > ( IQueryable < TResult > query )
156+ {
157+ // TODO: do we need to deal with the case where the user passes in "Name desc", should we strip the desc out, or let it override the isDescending param, or not deal with it and blame it on the user?
158+
159+ IOrderedQueryable < TResult > sortedQuery = null ;
160+
161+ if ( _primarySortAction != null )
162+ {
163+ sortedQuery = _primarySortAction ( query ) ;
164+ }
165+
166+ return _sortActions . Aggregate ( sortedQuery , ( current , sortAction ) => sortAction ( current ) ) ;
167+ }
168+
150169 /// <summary>
151170 /// Used in compiling a unique key for a query
152171 /// </summary>
153172 /// <returns>Unique key for a query</returns>
154173 public override string ToString ( )
155174 {
156- var val = String . Format ( "SortingOptions<{0}>\n Sort: {1}\n Extra: {2}" ,
157- ( typeof ( T ) ) . Name ,
158- _primarySortToString ?? "null" ,
159- String . Join ( "-" , _sortActionsToString )
175+ var val = string . Format ( "SortingOptions<{0}>\n Sort: {1}\n Extra: {2}" ,
176+ typeof ( T ) . Name ,
177+ _primarySortToString ?? "null" ,
178+ string . Join ( "-" , _sortActionsToString )
160179 ) ;
161180 return val ;
162181 }
0 commit comments