1- using System ;
2- using System . Linq ;
3- using System . Linq . Expressions ;
4- using SharpRepository . Repository . Linq ;
5-
6- namespace SharpRepository . Repository . Queries
7- {
8- /// <summary>
9- /// Used to define the sorting on queries run against a repository.
10- /// </summary>
11- /// <typeparam name="T">The entity type of the repository.</typeparam>
12- /// <typeparam name="TSortKey">The type of the property that is being sorted.</typeparam>
13- public class SortingOptions < T , TSortKey > : IQueryOptions < T >
14- {
15- public Expression < Func < T , TSortKey > > SortExpression { get ; internal set ; }
16- public bool IsDescending { get ; internal set ; }
17-
18- public SortingOptions ( Expression < Func < T , TSortKey > > sortExpression , bool isDescending = false )
19- {
20- SortExpression = sortExpression ;
21- IsDescending = isDescending ;
22- }
23-
24- /// <summary>
25- /// Applies sorting to the specified query.
26- /// </summary>
27- /// <param name="query">The query.</param>
28- /// <returns>Sorted results.</returns>
29- public virtual IQueryable < T > Apply ( IQueryable < T > query )
30- {
31- if ( SortExpression != null )
32- {
33- query = IsDescending
34- ? query . OrderByDescending ( SortExpression )
35- : query . OrderBy ( SortExpression ) ;
36- }
37-
38- return query ;
39- }
40-
41- /// <summary>
42- /// Used in compiling a unique key for a query
43- /// </summary>
44- /// <returns>Unique key for a query</returns>
45- public override string ToString ( )
46- {
47- return String . Format ( "SortingOptions<{0},{1}>\n Sort Expression: {2}\n IsDescending: {3}" ,
48- ( typeof ( T ) ) . Name ,
49- ( typeof ( TSortKey ) ) . Name ,
50- SortExpression == null ? "null" : SortExpression . ToString ( ) ,
51- IsDescending
52- ) ;
53- }
54- }
55-
56- /// <summary>
57- /// Used to define the sorting on queries run against a repository.
58- /// </summary>
59- /// <typeparam name="T">The entity type of the repository.</typeparam>
60- public class SortingOptions < T > : IQueryOptions < T >
61- {
62- public string SortProperty { get ; internal set ; }
63- public bool IsDescending { get ; internal set ; }
64-
65- public SortingOptions ( string sortProperty , bool isDescending = false )
66- {
67- SortProperty = sortProperty ;
68- IsDescending = isDescending ;
69- }
70-
71- /// <summary>
72- /// Applies sorting to the specified query.
73- /// </summary>
74- /// <param name="query">The query.</param>
75- /// <returns>Sorted results.</returns>
76- public virtual IQueryable < T > Apply ( IQueryable < T > query )
77- {
78- if ( ! String . IsNullOrEmpty ( SortProperty ) )
79- {
80- // 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?
81- var sortString = String . Format ( "{0}{1}" , SortProperty , IsDescending ? " desc" : "" ) ;
82- query = query . OrderBy ( sortString ) ;
83- }
84-
85- return query ;
86- }
87-
88- /// <summary>
89- /// Used in compiling a unique key for a query
90- /// </summary>
91- /// <returns>Unique key for a query</returns>
92- public override string ToString ( )
93- {
94- return String . Format ( "SortingOptions<{0}>\n Sort Property: {1}\n IsDescending: {2}" ,
95- ( typeof ( T ) ) . Name ,
96- SortProperty ,
97- IsDescending
98- ) ;
99- }
100- }
1+ using System ;
2+ using System . Linq ;
3+ using System . Linq . Expressions ;
4+ using SharpRepository . Repository . Linq ;
5+
6+ namespace SharpRepository . Repository . Queries
7+ {
8+ /// <summary>
9+ /// Used to define the sorting on queries run against a repository.
10+ /// </summary>
11+ /// <typeparam name="T">The entity type of the repository.</typeparam>
12+ /// <typeparam name="TSortKey">The type of the property that is being sorted.</typeparam>
13+ public class SortingOptions < T , TSortKey > : IQueryOptions < T >
14+ {
15+ public Expression < Func < T , TSortKey > > SortExpression { get ; set ; }
16+ public bool IsDescending { get ; set ; }
17+
18+ public SortingOptions ( Expression < Func < T , TSortKey > > sortExpression , bool isDescending = false )
19+ {
20+ SortExpression = sortExpression ;
21+ IsDescending = isDescending ;
22+ }
23+
24+ /// <summary>
25+ /// Applies sorting to the specified query.
26+ /// </summary>
27+ /// <param name="query">The query.</param>
28+ /// <returns>Sorted results.</returns>
29+ public virtual IQueryable < T > Apply ( IQueryable < T > query )
30+ {
31+ if ( SortExpression != null )
32+ {
33+ query = IsDescending
34+ ? query . OrderByDescending ( SortExpression )
35+ : query . OrderBy ( SortExpression ) ;
36+ }
37+
38+ return query ;
39+ }
40+
41+ /// <summary>
42+ /// Used in compiling a unique key for a query
43+ /// </summary>
44+ /// <returns>Unique key for a query</returns>
45+ public override string ToString ( )
46+ {
47+ return String . Format ( "SortingOptions<{0},{1}>\n Sort Expression: {2}\n IsDescending: {3}" ,
48+ ( typeof ( T ) ) . Name ,
49+ ( typeof ( TSortKey ) ) . Name ,
50+ SortExpression == null ? "null" : SortExpression . ToString ( ) ,
51+ IsDescending
52+ ) ;
53+ }
54+ }
55+
56+ /// <summary>
57+ /// Used to define the sorting on queries run against a repository.
58+ /// </summary>
59+ /// <typeparam name="T">The entity type of the repository.</typeparam>
60+ public class SortingOptions < T > : IQueryOptions < T >
61+ {
62+ public string SortProperty { get ; set ; }
63+ public bool IsDescending { get ; set ; }
64+
65+ public SortingOptions ( string sortProperty , bool isDescending = false )
66+ {
67+ SortProperty = sortProperty ;
68+ IsDescending = isDescending ;
69+ }
70+
71+ /// <summary>
72+ /// Applies sorting to the specified query.
73+ /// </summary>
74+ /// <param name="query">The query.</param>
75+ /// <returns>Sorted results.</returns>
76+ public virtual IQueryable < T > Apply ( IQueryable < T > query )
77+ {
78+ if ( ! String . IsNullOrEmpty ( SortProperty ) )
79+ {
80+ // 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?
81+ var sortString = String . Format ( "{0}{1}" , SortProperty , IsDescending ? " desc" : "" ) ;
82+ query = query . OrderBy ( sortString ) ;
83+ }
84+
85+ return query ;
86+ }
87+
88+ /// <summary>
89+ /// Used in compiling a unique key for a query
90+ /// </summary>
91+ /// <returns>Unique key for a query</returns>
92+ public override string ToString ( )
93+ {
94+ return String . Format ( "SortingOptions<{0}>\n Sort Property: {1}\n IsDescending: {2}" ,
95+ ( typeof ( T ) ) . Name ,
96+ SortProperty ,
97+ IsDescending
98+ ) ;
99+ }
100+ }
101101}
0 commit comments