c# Expression 扩展
一、简介
当查询比较复杂时,需要很多判断或者跨方法传递参数时使用
二、扩展类
/// <summary> /// Expression表达式扩展操作类 /// 调用方法:repository.GetAll().AsExpandable().Where(predicate) /// </summary> public static class ExpressionExtensions { /// <summary> /// 以特定的条件运行组合两个Expression表达式 /// </summary> /// <typeparam name="T">表达式的主实体类型</typeparam> /// <param name="first">第一个Expression表达式</param> /// <param name="second">要组合的Expression表达式</param> /// <param name="merge">组合条件运算方式</param> /// <returns>组合后的表达式</returns> public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) { var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f); var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); } /// <summary> /// 以 Expression.AndAlso 组合两个Expression表达式 /// </summary> /// <typeparam name="T">表达式的主实体类型</typeparam> /// <param name="first">第一个Expression表达式</param> /// <param name="second">要组合的Expression表达式</param> /// <returns>组合后的表达式</returns> public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.AndAlso); } /// <summary> /// 以 Expression.OrElse 组合两个Expression表达式 /// </summary> /// <typeparam name="T">表达式的主实体类型</typeparam> /// <param name="first">第一个Expression表达式</param> /// <param name="second">要组合的Expression表达式</param> /// <returns>组合后的表达式</returns> public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.OrElse); } private class ParameterRebinder : ExpressionVisitor { private readonly Dictionary<ParameterExpression, ParameterExpression> _map; private ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) { _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 node) { ParameterExpression replacement; if (_map.TryGetValue(node, out replacement)) node = replacement; return base.VisitParameter(node); } } }
三、如何使用
1.关于引用
using System.Linq;
using System.Linq.Expressions;
using LinqKit;
还需要引入扩展类的命名空间
2.使用示例
Expression<Func<User, bool>> pre; pre = s => s.NickName.Contains("李"); pre = pre.Or(s => s.NickName.Contains("陈")); pre = pre.And(s => s.CompanyId == "1");
var data = _userRepository.GetAll().AsExpandable().Where(pre);