@@ -11,16 +11,9 @@ namespace SharpRepository.Repository.Queries
1111 /// </summary>
1212 /// <typeparam name="T">The entity type of the repository.</typeparam>
1313 /// <typeparam name="TSortKey">The type of the property that is being sorted.</typeparam>
14- public class SortingOptions < T , TSortKey > : IQueryOptions < T >
14+ public class SortingOptions < T , TSortKey > : SortingOptions < T >
1515 {
16- private readonly Func < IQueryable < T > , IOrderedQueryable < T > > _primarySortAction ;
17- private readonly IList < Func < IOrderedQueryable < T > , IOrderedQueryable < T > > > _sortActions = new List < Func < IOrderedQueryable < T > , IOrderedQueryable < T > > > ( ) ;
18-
19- // we need these fields because calling ToString on the Func's didn't include the details of the method being called so it was the same key for descending and ascending
20- private readonly string _primarySortToString ;
21- private readonly IList < string > _sortActionsToString = new List < string > ( ) ;
22-
23- public SortingOptions ( Expression < Func < T , TSortKey > > sortExpression , bool isDescending = false )
16+ public SortingOptions ( Expression < Func < T , TSortKey > > sortExpression , bool isDescending = false ) : base ( sortExpression . ToString ( ) , isDescending )
2417 {
2518 if ( isDescending )
2619 {
@@ -30,57 +23,6 @@ public SortingOptions(Expression<Func<T, TSortKey>> sortExpression, bool isDesce
3023 {
3124 _primarySortAction = q => q . OrderBy ( sortExpression ) ;
3225 }
33-
34- _primarySortToString = String . Format ( "{0}-{1}" , sortExpression , isDescending ) ;
35- }
36-
37- public void ThenSortBy < TNewSortKey > ( Expression < Func < T , TNewSortKey > > sortExpression , bool isDescending = false )
38- {
39- Func < IOrderedQueryable < T > , IOrderedQueryable < T > > sortAction = null ;
40-
41- if ( isDescending )
42- {
43- sortAction = q => q . ThenByDescending ( sortExpression ) ;
44- }
45- else
46- {
47- sortAction = q => q . ThenBy ( sortExpression ) ;
48- }
49-
50- _sortActions . Add ( sortAction ) ;
51- _sortActionsToString . Add ( String . Format ( "{0}-{1}" , sortExpression , isDescending ) ) ;
52- }
53-
54-
55- /// <summary>
56- /// Applies sorting to the specified query.
57- /// </summary>
58- /// <param name="query">The query.</param>
59- /// <returns>Sorted results.</returns>
60- public virtual IQueryable < T > Apply ( IQueryable < T > query )
61- {
62- IOrderedQueryable < T > sortedQuery = null ;
63-
64- if ( _primarySortAction != null )
65- {
66- sortedQuery = _primarySortAction ( query ) ;
67- }
68-
69- return _sortActions . Aggregate ( sortedQuery , ( current , sortAction ) => sortAction ( current ) ) ;
70- }
71-
72- /// <summary>
73- /// Used in compiling a unique key for a query
74- /// </summary>
75- /// <returns>Unique key for a query</returns>
76- public override string ToString ( )
77- {
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 )
83- ) ;
8426 }
8527 }
8628
@@ -90,12 +32,12 @@ public override string ToString()
9032 /// <typeparam name="T">The entity type of the repository.</typeparam>
9133 public class SortingOptions < T > : IQueryOptions < T >
9234 {
93- private readonly Func < IQueryable < T > , IOrderedQueryable < T > > _primarySortAction ;
94- private readonly IList < Func < IOrderedQueryable < T > , IOrderedQueryable < T > > > _sortActions = new List < Func < IOrderedQueryable < T > , IOrderedQueryable < T > > > ( ) ;
35+ protected Func < IQueryable < T > , IOrderedQueryable < T > > _primarySortAction ;
36+ protected IList < Func < IOrderedQueryable < T > , IOrderedQueryable < T > > > _sortActions = new List < Func < IOrderedQueryable < T > , IOrderedQueryable < T > > > ( ) ;
9537
9638 // we need these fields because calling ToString on the Func's didn't include the details of the method being called so it was the same key for descending and ascending
97- private readonly string _primarySortToString ;
98- private readonly IList < string > _sortActionsToString = new List < string > ( ) ;
39+ protected string _primarySortToString ;
40+ protected IList < string > _sortActionsToString = new List < string > ( ) ;
9941
10042 public SortingOptions ( string sortProperty , bool isDescending = false )
10143 {
@@ -108,7 +50,7 @@ public SortingOptions(string sortProperty, bool isDescending = false)
10850 _primarySortAction = q => q . OrderByProperty ( sortProperty ) ;
10951 }
11052
111- _primarySortToString = String . Format ( "{0}-{1}" , sortProperty , isDescending ) ;
53+ _primarySortToString = string . Format ( "{0}-{1}" , sortProperty , isDescending ) ;
11254 }
11355
11456 public void ThenSortBy ( string sortProperty , bool isDescending = false )
@@ -128,6 +70,24 @@ public void ThenSortBy(string sortProperty, bool isDescending = false)
12870 _sortActionsToString . Add ( String . Format ( "{0}-{1}" , sortProperty , isDescending ) ) ;
12971 }
13072
73+ public void ThenSortBy < TNewSortKey > ( Expression < Func < T , TNewSortKey > > sortExpression , bool isDescending = false )
74+ {
75+ Func < IOrderedQueryable < T > , IOrderedQueryable < T > > sortAction = null ;
76+
77+ if ( isDescending )
78+ {
79+ sortAction = q => q . ThenByDescending ( sortExpression ) ;
80+ }
81+ else
82+ {
83+ sortAction = q => q . ThenBy ( sortExpression ) ;
84+ }
85+
86+ _sortActions . Add ( sortAction ) ;
87+ _sortActionsToString . Add ( String . Format ( "{0}-{1}" , sortExpression , isDescending ) ) ;
88+ }
89+
90+
13191 /// <summary>
13292 /// Applies sorting to the specified query.
13393 /// </summary>
@@ -137,12 +97,9 @@ public virtual IQueryable<T> Apply(IQueryable<T> query)
13797 {
13898 // 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?
13999
140- IOrderedQueryable < T > sortedQuery = null ;
141-
142- if ( _primarySortAction != null )
143- {
144- sortedQuery = _primarySortAction ( query ) ;
145- }
100+ IOrderedQueryable < T > sortedQuery = _primarySortAction != null
101+ ? _primarySortAction ( query )
102+ : null ;
146103
147104 return _sortActions . Aggregate ( sortedQuery , ( current , sortAction ) => sortAction ( current ) ) ;
148105 }
@@ -153,10 +110,10 @@ public virtual IQueryable<T> Apply(IQueryable<T> query)
153110 /// <returns>Unique key for a query</returns>
154111 public override string ToString ( )
155112 {
156- var val = String . Format ( "SortingOptions<{0}>\n Sort: {1}\n Extra: {2}" ,
157- ( typeof ( T ) ) . Name ,
113+ var val = string . Format ( "SortingOptions<{0}>\n Sort: {1}\n Extra: {2}" ,
114+ typeof ( T ) . Name ,
158115 _primarySortToString ?? "null" ,
159- String . Join ( "-" , _sortActionsToString )
116+ string . Join ( "-" , _sortActionsToString )
160117 ) ;
161118 return val ;
162119 }
0 commit comments