标签:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Linq.Expressions; 5 using System.Web; 6 7 namespace Test 8 { 9 /// <summary> 10 /// 统一ParameterExpression 11 /// </summary> 12 public class ParameterReplacer : ExpressionVisitor 13 { 14 public ParameterReplacer(ParameterExpression paramExpr) 15 { 16 ParameterExpression = paramExpr; 17 } 18 19 public ParameterExpression ParameterExpression { get; private set; } 20 21 public Expression Replace(Expression expr) 22 { 23 return Visit(expr); 24 } 25 26 protected override Expression VisitParameter(ParameterExpression p) 27 { 28 return ParameterExpression; 29 } 30 } 31 /// <summary> 32 /// Predicate扩展 33 /// </summary> 34 public static class PredicateExtensionses 35 { 36 /// <summary> 37 /// 38 /// </summary> 39 /// <typeparam name="T"></typeparam> 40 /// <returns></returns> 41 public static Expression<Func<T, bool>> True<T>() { return f => true; } 42 /// <summary> 43 /// 44 /// </summary> 45 /// <typeparam name="T"></typeparam> 46 /// <returns></returns> 47 public static Expression<Func<T, bool>> False<T>() { return f => false; } 48 /// <summary> 49 /// 50 /// </summary> 51 /// <typeparam name="T"></typeparam> 52 /// <param name="expLeft"></param> 53 /// <param name="expRight"></param> 54 /// <returns></returns> 55 public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight) 56 { 57 var candidateExpr = Expression.Parameter(typeof(T), "candidate"); 58 var parameterReplacer = new ParameterReplacer(candidateExpr); 59 60 var left = parameterReplacer.Replace(expLeft.Body); 61 var right = parameterReplacer.Replace(expRight.Body); 62 var body = Expression.And(left, right); 63 64 return Expression.Lambda<Func<T, bool>>(body, candidateExpr); 65 } 66 /// <summary> 67 /// 68 /// </summary> 69 /// <typeparam name="T"></typeparam> 70 /// <param name="expLeft"></param> 71 /// <param name="expRight"></param> 72 /// <returns></returns> 73 public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight) 74 { 75 var candidateExpr = Expression.Parameter(typeof(T), "candidate"); 76 var parameterReplacer = new ParameterReplacer(candidateExpr); 77 78 var left = parameterReplacer.Replace(expLeft.Body); 79 var right = parameterReplacer.Replace(expRight.Body); 80 var body = Expression.Or(left, right); 81 82 return Expression.Lambda<Func<T, bool>>(body, candidateExpr); 83 } 84 } 85 /// <summary> 86 /// Queryable扩展 87 /// </summary> 88 public static class QueryableExtensions 89 { 90 /// <summary> 91 /// 92 /// </summary> 93 /// <typeparam name="T"></typeparam> 94 /// <param name="queryable"></param> 95 /// <param name="propertyName"></param> 96 /// <returns></returns> 97 public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName) 98 { 99 return OrderBy(queryable, propertyName, false); 100 } 101 /// <summary> 102 /// OrderBy 103 /// </summary> 104 /// <typeparam name="T">实体</typeparam> 105 /// <param name="queryable">条件</param> 106 /// <param name="propertyName">属性名称</param> 107 /// <param name="desc">是否降序</param> 108 /// <returns></returns> 109 public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName, bool desc) 110 { 111 var param = Expression.Parameter(typeof(T)); 112 var body = Expression.Property(param, propertyName); 113 dynamic keySelector = Expression.Lambda(body, param); 114 return desc ? Queryable.OrderByDescending(queryable, keySelector) : Queryable.OrderBy(queryable, keySelector); 115 } 116 } 117 }
标签:
原文地址:http://www.cnblogs.com/china-chang/p/5865694.html