public static class PredicateBuilder {
/// <summary>
/// 可以理解为创建一个初始化为True的Predicate。
/// 注意:如果你是要创建一个OR组成的Predicate就不能把它初始化为True因为这样这个表达试永远为True了。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> True<T>() { return f => true; }
/// <summary>
/// 可以理解为创建一个初始化为False的Predicate。
///注意:如果你是要创建一个AND组成的Predicate就不能把它初始化为False因为这样这个表达试永远为False了。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) { // build parameter map (from parameters of second to parameters of first) var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f); // replace parameters in the second lambda expression with parameters from the first var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); // apply composition of lambda expression bodies to parameters from the first expression return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.And); } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.Or); } class ParameterRebinder : ExpressionVisitor { private readonly Dictionary<ParameterExpression, ParameterExpression> map; public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) { this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>(); } public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) { return new ParameterRebinder(map).Visit(exp); } protected override Expression VisitParameter(ParameterExpression p) { ParameterExpression replacement; if (map.TryGetValue(p, out replacement)) { p = replacement; } return base.VisitParameter(p); } } }
调用方法
/* Lambda 表达式查询条件拼接 调用实例 var predicate = PredicateBuilder.True<Orders>(); if (!string.IsNullOrWhiteSpace(select.so_text)) { predicate = predicate.And(d => d.price > 10); predicate = predicate.And(d => d.price < 12); } var list = db.Orders.Where(predicate).ToList();
全And:
var where = PredicateBuilder.True<int>();
where = where.And(x => x >= 50);
where = where.And(x => x <= 70);
var res = list.Where(where.Compile());
全Or:
var list = Enumerable.Range(1, 100);
var where = PredicateBuilder.False<int>();
where = where.Or(x => x == 50);
where = where.Or(x => x == 70);
var res = list.Where(where.Compile());
var list = Enumerable.Range(1, 100);
var where = PredicateBuilder.True<int>();
where = where.And(x => x >= 50);
where = where.And(x => x <= 70);
var subwhere = PredicateBuilder.False<int>();
subwhere = subwhere.Or(x => x == 60);
subwhere = subwhere.Or(x => x == 61);
where = where.And(subwhere);
var res = list.Where(where.Compile());
*/