public static class ExpressionHandle { /// <summary> /// Linq的And操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="first"></param> /// <param name="second"></param> /// <param name="tf">是否拼接param> /// <returns>val为空就返回first,否则返回first与second的And操作</returns> public static Expression<Func<T, bool>> IfAnd<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, bool tf) { if (tf) { return first.ExpressionAnd(second); } return first; } /// <summary> /// Linq的Or操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="first"></param> /// <param name="second"></param> /// <param name="tf">是否为拼接</param> /// <returns>val为空就返回first,否则返回first与second的Or操作</returns> public static Expression<Func<T, bool>> IfOr<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, bool tf) { if (tf) { return first.ExpressionOr(second); } return first; } /// <summary> /// Linq的And操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="first"></param> /// <param name="second"></param> /// <param name="tf">是否拼接param> /// <returns>val为空就返回first,否则返回first与second的And操作</returns> public static Expression<Func<T1, T2, bool>> IfAnd<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second, bool tf) { if (tf) { return first.ExpressionAnd(second); } return first; } /// <summary> /// Linq的Or操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="first"></param> /// <param name="second"></param> /// <param name="tf">是否为拼接</param> /// <returns>val为空就返回first,否则返回first与second的Or操作</returns> public static Expression<Func<T1, T2, bool>> IfOr<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second, bool tf) { if (tf) { return first.ExpressionOr(second); } return first; } /// <summary> /// Linq的And操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="first"></param> /// <param name="second"></param> /// <param name="tf">是否拼接param> /// <returns>val为空就返回first,否则返回first与second的And操作</returns> public static Expression<Func<T1, T2, T3, bool>> IfAnd<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second, bool tf) { if (tf) { return first.ExpressionAnd(second); } return first; } /// <summary> /// Linq的Or操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="first"></param> /// <param name="second"></param> /// <param name="tf">是否为拼接</param> /// <returns>val为空就返回first,否则返回first与second的Or操作</returns> public static Expression<Func<T1, T2, T3, bool>> IfOr<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second, bool tf) { if (tf) { return first.ExpressionOr(second); } return first; } }
public static class ExpressionHelper { private static Expression<T> Combine<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> /// And合并 /// </summary> /// <returns></returns> public static Expression<Func<T, bool>> ExpressionAnd<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Combine(second, Expression.And); } /// <summary> /// OR合并 /// </summary> /// <returns></returns> public static Expression<Func<T, bool>> ExpressionOr<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Combine(second, Expression.Or); } /// <summary> /// And合并 /// </summary> /// <returns></returns> public static Expression<Func<T1, T2, bool>> ExpressionAnd<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second) { return first.Combine(second, Expression.And); } /// <summary> /// OR合并 /// </summary> /// <returns></returns> public static Expression<Func<T1, T2, bool>> ExpressionOr<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second) { return first.Combine(second, Expression.Or); } /// <summary> /// And合并 /// </summary> /// <returns></returns> public static Expression<Func<T1, T2, T3, bool>> ExpressionAnd<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second) { return first.Combine(second, Expression.And); } /// <summary> /// OR合并 /// </summary> /// <returns></returns> public static Expression<Func<T1, T2, T3, bool>> ExpressionOr<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second) { return first.Combine(second, Expression.Or); } }
public class ParameterRebinder : ExpressionVisitor { private readonly Dictionary<ParameterExpression, ParameterExpression> map; /// <summary> /// /// </summary> /// <param name="map"></param> public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) { this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>(); } /// <summary> /// /// </summary> /// <param name="map"></param> /// <param name="exp"></param> /// <returns></returns> public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) { return new ParameterRebinder(map).Visit(exp); } /// <summary> /// /// </summary> /// <param name="p"></param> /// <returns></returns> protected override Expression VisitParameter(ParameterExpression p) { ParameterExpression replacement; if (map.TryGetValue(p, out replacement)) { p = replacement; } return base.VisitParameter(p); } }
标签:return,C#,public,second,static,Expression,好用,first From: https://www.cnblogs.com/wang920607/p/18135340