You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

46 lines
2.1 KiB

using System.Linq;
using System.Linq.Expressions;
namespace Znyc.Cloudcar.Admin.Commons.Extend
{
/// <summary>
/// </summary>
public static class ExtQueryable
{
/// <summary>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="ordering"></param>
/// <returns></returns>
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering)
{
System.Type type = typeof(T);
System.Reflection.PropertyInfo property = type.GetProperty(ordering);
ParameterExpression parameter = Expression.Parameter(type, "p");
MemberExpression propertyAccess = Expression.MakeMemberAccess(parameter, property);
LambdaExpression orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp = Expression.Call(typeof(Queryable), "OrderBy", new[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);
}
/// <summary>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="ordering"></param>
/// <returns></returns>
public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string ordering)
{
System.Type type = typeof(T);
System.Reflection.PropertyInfo property = type.GetProperty(ordering);
ParameterExpression parameter = Expression.Parameter(type, "p");
MemberExpression propertyAccess = Expression.MakeMemberAccess(parameter, property);
LambdaExpression orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp = Expression.Call(typeof(Queryable), "OrderByDescending",
new[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);
}
}
}