File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
src/jQueryDatatableServerSideNetCore/Extensions Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Linq ;
3+ using System . Linq . Expressions ;
4+ using jQueryDatatableServerSideNetCore . Models . AuxiliaryModels ;
5+
6+ namespace jQueryDatatableServerSideNetCore . Extensions
7+ {
8+ public static class LinqExtensions
9+ {
10+ public static IQueryable < T > OrderByDynamic < T > (
11+ this IQueryable < T > query ,
12+ string orderByMember ,
13+ DtOrderDir ascendingDirection )
14+ {
15+ var param = Expression . Parameter ( typeof ( T ) , "c" ) ;
16+
17+ var body = orderByMember . Split ( '.' ) . Aggregate < string , Expression > ( param , Expression . PropertyOrField ) ;
18+
19+ var queryable = ascendingDirection == DtOrderDir . Asc ?
20+ ( IOrderedQueryable < T > ) Queryable . OrderBy ( query . AsQueryable ( ) , ( dynamic ) Expression . Lambda ( body , param ) ) :
21+ ( IOrderedQueryable < T > ) Queryable . OrderByDescending ( query . AsQueryable ( ) , ( dynamic ) Expression . Lambda ( body , param ) ) ;
22+
23+ return queryable ;
24+ }
25+
26+ public static IQueryable < T > WhereDynamic < T > (
27+ this IQueryable < T > sourceList , string query )
28+ {
29+
30+ if ( string . IsNullOrEmpty ( query ) )
31+ {
32+ return sourceList ;
33+ }
34+
35+ try
36+ {
37+
38+ var properties = typeof ( T ) . GetProperties ( )
39+ . Where ( x => x . CanRead && x . CanWrite && ! x . GetGetMethod ( ) . IsVirtual ) ;
40+
41+ //Expression
42+ sourceList = sourceList . Where ( c =>
43+ properties . Any ( p => p . GetValue ( c ) != null && p . GetValue ( c ) . ToString ( )
44+ . Contains ( query , StringComparison . InvariantCultureIgnoreCase ) ) ) ;
45+ }
46+ catch ( Exception e )
47+ {
48+ Console . WriteLine ( e ) ;
49+ }
50+
51+ return sourceList ;
52+ }
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments