Skip to content

Instantly share code, notes, and snippets.

@smiler
Created September 28, 2012 14:24
Show Gist options
  • Save smiler/3800162 to your computer and use it in GitHub Desktop.
Save smiler/3800162 to your computer and use it in GitHub Desktop.
public static IQueryable<T> Order<T>( this IQueryable<T> source, IOrderDetails co ) {
if( co == null || co.GetOrderColumn() == null )
return source;
try {
var x = Expression.Parameter( source.ElementType, "x" );
var selector = Expression.Lambda( Expression.PropertyOrField( x, co.GetOrderColumn() ), x );
return source.Provider.CreateQuery(
Expression.Call( typeof( Queryable ), co.IsDescending() ? "OrderByDescending" : "OrderBy", new Type[] { source.ElementType, selector.Body.Type },
source.Expression, selector
) ) as IQueryable<T>;
} catch {
return source;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment