Private StaticIqueryable<t> setqueryableorder<t> ( Thisiqueryable<t> Query,stringSortstringorder) { if(string. IsNullOrEmpty (sort))Throw NewException ("You must specify a sort field!"); PropertyInfo SortProperty=typeof(T). GetProperty (Sort, bindingflags.public | BindingFlags.Instance |bindingflags.ignorecase); if(SortProperty = =NULL) Throw NewException ("the sort field does not exist in the query object"+ Sort +"! "); ParameterExpression param= Expression.parameter (typeof(T),"T"); Expression Body=param; if(Nullable.getunderlyingtype (body). Type)! =NULL) Body= Expression.property (Body,"Value"); Body=expression.makememberaccess (body, sortproperty); LambdaExpression Keyselectorlambda=expression.lambda (body, param); if(string. IsNullOrEmpty (order)) Order="ASC"; stringQuerymethod = order. ToUpper () = ="DESC"?"orderbydescending":" by"; Query= Query. Provider.createquery<t> (Expression.call (typeof(queryable), Querymethod,NewType[] {typeof(T), body. Type}, query. Expression, Expression.quote (KEYSELECTORLAMBDA)); returnquery; }
Popular Science: extension methods. Must be static and the first parameter that begins is a type preceded by this
The EF notation that can be sorted by passing in the specified field name