1
1
/*
2
- megridview v0.3.1
2
+ megridview v0.4.0
3
3
Developed By Mesut Talebi (mesut.talebi@yahoo.com)
4
4
Open Source And no licence :) free to use
5
5
*/
6
6
7
7
8
8
using System ;
9
9
using System . Collections . Generic ;
10
+ using System . Linq ;
11
+ using System . Linq . Expressions ;
12
+ using System . Reflection ;
10
13
11
14
namespace MT . GridView . Models
12
15
{
@@ -22,7 +25,7 @@ public class PagingInfo
22
25
23
26
public int TotalPages
24
27
{
25
- get { return ( int ) Math . Ceiling ( ( decimal ) TotalItems / ( ItemsPerPage != 0 ? ItemsPerPage : 1 ) ) ; }
28
+ get { return ( int ) Math . Ceiling ( ( decimal ) TotalItems / ( ItemsPerPage != 0 ? ItemsPerPage : 1 ) ) ; }
26
29
}
27
30
28
31
public SortObject Sort { get ; set ; }
@@ -41,7 +44,7 @@ public class SortObject
41
44
42
45
public class FilterObject
43
46
{
44
- public string Column { get ; set ; }
47
+ public string Column { get ; set ; }
45
48
46
49
public string Value { get ; set ; }
47
50
@@ -78,60 +81,156 @@ public enum FilterConjunction
78
81
Or
79
82
}
80
83
81
- public class Extensions
84
+ public static class Extensions
82
85
{
86
+ private static LambdaExpression GetExpression < T > ( string propertyName , string methodName , out Type type )
87
+ {
88
+ string [ ] props = propertyName . Split ( '.' ) ;
89
+ type = typeof ( T ) ;
90
+
91
+ ParameterExpression arg = Expression . Parameter ( type , "x" ) ;
92
+ Expression expr = arg ;
93
+
94
+ foreach ( string prop in props )
95
+ {
96
+ // use reflection (not ComponentModel) to mirror LINQ
97
+ PropertyInfo pi = type . GetProperty ( prop ) ;
98
+ expr = Expression . Property ( expr , pi ) ;
99
+ type = pi . PropertyType ;
100
+ }
101
+
102
+ Type delegateType = typeof ( Func < , > ) . MakeGenericType ( typeof ( T ) , type ) ;
103
+
104
+ return Expression . Lambda ( delegateType , expr , arg ) ;
105
+ }
106
+
107
+ private static IOrderedEnumerable < T > ApplyOrder < T > (
108
+ IEnumerable < T > source ,
109
+ string propertyName ,
110
+ string methodName )
111
+ {
112
+ Type type = typeof ( T ) ;
113
+ LambdaExpression lambda = GetExpression < T > ( propertyName , methodName , out type ) ;
114
+
115
+ object result = typeof ( Queryable ) . GetMethods ( ) . Single (
116
+ method => method . Name == methodName
117
+ && method . IsGenericMethodDefinition
118
+ && method . GetGenericArguments ( ) . Length == 2
119
+ && method . GetParameters ( ) . Length == 2 )
120
+ . MakeGenericMethod ( typeof ( T ) , type )
121
+ . Invoke ( null , new object [ ] { source , lambda } ) ;
122
+
123
+ return ( IOrderedEnumerable < T > ) result ;
124
+ }
125
+
126
+
127
+ private static IOrderedQueryable < T > ApplyOrder < T > (
128
+ IQueryable < T > source ,
129
+ string propertyName , string methodName )
130
+ {
131
+ Type type = typeof ( T ) ;
132
+ LambdaExpression lambda = GetExpression < T > ( propertyName , methodName , out type ) ;
133
+
134
+ object result = typeof ( Queryable ) . GetMethods ( ) . Single (
135
+ method => method . Name == methodName
136
+ && method . IsGenericMethodDefinition
137
+ && method . GetGenericArguments ( ) . Length == 2
138
+ && method . GetParameters ( ) . Length == 2 )
139
+ . MakeGenericMethod ( typeof ( T ) , type )
140
+ . Invoke ( null , new object [ ] { source , lambda } ) ;
141
+
142
+ return ( IOrderedQueryable < T > ) result ;
143
+ }
144
+
145
+ // IQueryable Sort Extention
146
+ public static IOrderedQueryable < TSource > Sort < TSource > ( this IQueryable < TSource > source , SortObject sortObject )
147
+ {
148
+ if ( sortObject != null )
149
+ {
150
+ switch ( sortObject . Direction )
151
+ {
152
+ case SortDirection . Ascending :
153
+ return ApplyOrder < TSource > ( source , sortObject . SortColumn , "OrderBy" ) ;
154
+ case SortDirection . Descending :
155
+ return ApplyOrder < TSource > ( source , sortObject . SortColumn , "OrderByDescending" ) ;
156
+ default :
157
+ break ;
158
+ }
159
+ }
160
+
161
+ return source as IOrderedQueryable < TSource > ;
162
+ }
163
+
164
+ public static IOrderedEnumerable < TSource > Sort < TSource > ( this IEnumerable < TSource > source , SortObject sortObject )
165
+ {
166
+ if ( sortObject != null )
167
+ {
168
+ switch ( sortObject . Direction )
169
+ {
170
+ case SortDirection . Ascending :
171
+ return ApplyOrder < TSource > ( source , sortObject . SortColumn , "OrderBy" ) ;
172
+ case SortDirection . Descending :
173
+ return ApplyOrder < TSource > ( source , sortObject . SortColumn , "OrderByDescending" ) ;
174
+ default :
175
+ break ;
176
+ }
177
+ }
178
+
179
+ return source as IOrderedEnumerable < TSource > ;
180
+ }
181
+
83
182
public static string GetWhereClause ( FilterObject filterObj , Type valueType )
84
- {
183
+ {
85
184
string whereClause = "true" ;
86
- if ( valueType != typeof ( DateTime ) )
185
+ if ( valueType != typeof ( DateTime ) )
87
186
{
88
187
switch ( filterObj . Operator )
89
188
{
90
189
case FilterOperator . Contains :
91
- if ( valueType == typeof ( string ) )
190
+ if ( valueType == typeof ( string ) )
92
191
whereClause += string . Format ( " {0} {1}.Contains(\" {2}\" )" , filterObj . Conjunction ,
93
192
filterObj . Column , filterObj . Value ) ;
94
193
break ;
95
194
case FilterOperator . GreaterThan :
96
- if ( valueType != typeof ( string ) )
195
+ if ( valueType != typeof ( string ) )
97
196
whereClause += string . Format ( " {0} {1} > {2}" , filterObj . Conjunction , filterObj . Column ,
98
197
filterObj . Value ) ;
99
198
break ;
100
199
case FilterOperator . GreaterThanOrEqual :
101
- if ( valueType != typeof ( string ) )
200
+ if ( valueType != typeof ( string ) )
102
201
whereClause += string . Format ( " {0} {1} >= {2}" , filterObj . Conjunction , filterObj . Column ,
103
202
filterObj . Value ) ;
104
203
break ;
105
204
case FilterOperator . LessThan :
106
- if ( valueType != typeof ( string ) )
205
+ if ( valueType != typeof ( string ) )
107
206
whereClause += string . Format ( " {0} {1} < {2}" , filterObj . Conjunction , filterObj . Column ,
108
207
filterObj . Value ) ;
109
208
break ;
110
209
case FilterOperator . LessThanOrEqual :
111
- if ( valueType != typeof ( string ) )
210
+ if ( valueType != typeof ( string ) )
112
211
whereClause += string . Format ( " {0} {1} <= {2}" , filterObj . Conjunction , filterObj . Column ,
113
212
filterObj . Value ) ;
114
213
break ;
115
214
case FilterOperator . StartsWith :
116
- if ( valueType != typeof ( string ) )
215
+ if ( valueType != typeof ( string ) )
117
216
whereClause += string . Format ( " {0} {1}.StartsWith(\" {2}\" )" , filterObj . Conjunction ,
118
217
filterObj . Column , filterObj . Value ) ;
119
218
break ;
120
219
case FilterOperator . EndsWith :
121
- if ( valueType != typeof ( string ) )
220
+ if ( valueType != typeof ( string ) )
122
221
whereClause += string . Format ( " {0} {1}.EndsWith(\" {2}\" )" , filterObj . Conjunction ,
123
222
filterObj . Column , filterObj . Value ) ;
124
223
break ;
125
224
case FilterOperator . Equals :
126
225
127
226
whereClause +=
128
- string . Format ( valueType == typeof ( string ) ? " {0} {1} == \" {2}\" " : " {0} {1} == {2}" ,
227
+ string . Format ( valueType == typeof ( string ) ? " {0} {1} == \" {2}\" " : " {0} {1} == {2}" ,
129
228
filterObj . Conjunction , filterObj . Column , filterObj . Value ) ;
130
229
break ;
131
230
case FilterOperator . NotEqual :
132
231
133
232
whereClause +=
134
- string . Format ( valueType == typeof ( string ) ? " {0} {1} != \" {2}\" " : " {0} {1} != {2}" ,
233
+ string . Format ( valueType == typeof ( string ) ? " {0} {1} != \" {2}\" " : " {0} {1} != {2}" ,
135
234
filterObj . Conjunction , filterObj . Column , filterObj . Value ) ;
136
235
break ;
137
236
default :
@@ -145,11 +244,11 @@ public static string GetWhereClause(FilterObject filterObj, Type valueType)
145
244
146
245
switch ( filterObj . Operator )
147
246
{
148
- case FilterOperator . Contains :
247
+ case FilterOperator . Contains :
149
248
break ;
150
249
case FilterOperator . GreaterThan :
151
-
152
- whereClause += string . Format ( " {0} {1} > DateTime(\" {2}\" )" , filterObj . Conjunction , filterObj . Column , dt ) ;
250
+
251
+ whereClause += string . Format ( " {0} {1} > DateTime(\" {2}\" )" , filterObj . Conjunction , filterObj . Column , dt ) ;
153
252
break ;
154
253
case FilterOperator . GreaterThanOrEqual :
155
254
@@ -162,9 +261,9 @@ public static string GetWhereClause(FilterObject filterObj, Type valueType)
162
261
case FilterOperator . LessThanOrEqual :
163
262
whereClause += string . Format ( " {0} {1} <= DateTime(\" {2}\" )" , filterObj . Conjunction , filterObj . Column , dt ) ;
164
263
break ;
165
- case FilterOperator . StartsWith :
264
+ case FilterOperator . StartsWith :
166
265
break ;
167
- case FilterOperator . EndsWith :
266
+ case FilterOperator . EndsWith :
168
267
break ;
169
268
case FilterOperator . Equals :
170
269
whereClause += string . Format ( " {0} {1} == DateTime(\" {2}\" )" , filterObj . Conjunction , filterObj . Column , dt ) ;
0 commit comments