/// <summary> /// 统一ParameterExpression /// </summary> internal class ParameterReplacer : ExpressionVisitor { public ParameterReplacer(ParameterExpression paramExpr) { ParameterExpression = paramExpr; } public ParameterExpression ParameterExpression { get; private set; } public Expression Replace(Expression expr) { return Visit(expr); } protected override Expression VisitParameter(ParameterExpression p) { return ParameterExpression; } } /// <summary> /// Predicate扩展 /// </summary> public static class PredicateExtensionses { /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static Expression<Func<T, bool>> True<T>() { return f => true; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static Expression<Func<T, bool>> False<T>() { return f => false; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expLeft"></param> /// <param name="expRight"></param> /// <returns></returns> public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); var parameterReplacer = new ParameterReplacer(candidateExpr); var left = parameterReplacer.Replace(expLeft.Body); var right = parameterReplacer.Replace(expRight.Body); var body = Expression.And(left, right); return Expression.Lambda<Func<T, bool>>(body, candidateExpr); } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expLeft"></param> /// <param name="expRight"></param> /// <returns></returns> public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); var parameterReplacer = new ParameterReplacer(candidateExpr); var left = parameterReplacer.Replace(expLeft.Body); var right = parameterReplacer.Replace(expRight.Body); var body = Expression.Or(left, right); return Expression.Lambda<Func<T, bool>>(body, candidateExpr); } } /// <summary> /// Queryable扩展 /// </summary> public static class QueryableExtensions { /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="propertyName"></param> /// <returns></returns> public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName) { return OrderBy(queryable, propertyName, false); } /// <summary> /// OrderBy /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="queryable">条件</param> /// <param name="propertyName">属性名称</param> /// <param name="desc">是否降序</param> /// <returns></returns> public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName, bool desc) { var param = Expression.Parameter(typeof(T)); var body = Expression.Property(param, propertyName); dynamic keySelector = Expression.Lambda(body, param); return desc ? Queryable.OrderByDescending(queryable, keySelector) : Queryable.OrderBy(queryable, keySelector); } }