PredicateExtensions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WebAPIBase.Utils
  8. {
  9. /// <summary>
  10. /// lambda表达式拼接扩展类
  11. /// </summary>
  12. public static class PredicateExtensions
  13. {
  14. /// <summary>
  15. /// Creates a predicate that evaluates to true.
  16. /// </summary>
  17. public static Expression<Func<T, bool>> True<T>() { return param => true; }
  18. /// <summary>
  19. /// Creates a predicate that evaluates to false.
  20. /// </summary>
  21. public static Expression<Func<T, bool>> False<T>() { return param => false; }
  22. /// <summary>
  23. /// Creates a predicate expression from the specified lambda expression.
  24. /// </summary>
  25. public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; }
  26. /// <summary>
  27. /// Combines the first predicate with the second using the logical "and".
  28. /// </summary>
  29. public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
  30. {
  31. return first.Compose(second, Expression.AndAlso);
  32. }
  33. /// <summary>
  34. /// Combines the first predicate with the second using the logical "or".
  35. /// </summary>
  36. public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
  37. {
  38. return first.Compose(second, Expression.OrElse);
  39. }
  40. /// <summary>
  41. /// Negates the predicate.
  42. /// </summary>
  43. public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
  44. {
  45. var negated = Expression.Not(expression.Body);
  46. return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
  47. }
  48. /// <summary>
  49. /// Combines the first expression with the second using the specified merge function.
  50. /// </summary>
  51. static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
  52. {
  53. // zip parameters (map from parameters of second to parameters of first)
  54. var map = first.Parameters
  55. .Select((f, i) => new { f, s = second.Parameters[i] })
  56. .ToDictionary(p => p.s, p => p.f);
  57. // replace parameters in the second lambda expression with the parameters in the first
  58. var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
  59. // create a merged lambda expression with parameters from the first expression
  60. return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
  61. }
  62. /// <summary>
  63. /// ParameterRebinder
  64. /// </summary>
  65. class ParameterRebinder : ExpressionVisitor
  66. {
  67. /// <summary>
  68. /// The ParameterExpression map
  69. /// </summary>
  70. readonly Dictionary<ParameterExpression, ParameterExpression> map;
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
  73. /// </summary>
  74. /// <param name="map">The map.</param>
  75. ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
  76. {
  77. this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
  78. }
  79. /// <summary>
  80. /// Replaces the parameters.
  81. /// </summary>
  82. /// <param name="map">The map.</param>
  83. /// <param name="exp">The exp.</param>
  84. /// <returns>Expression</returns>
  85. public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
  86. {
  87. return new ParameterRebinder(map).Visit(exp);
  88. }
  89. /// <summary>
  90. /// Visits the parameter.
  91. /// </summary>
  92. /// <param name="p">The p.</param>
  93. /// <returns>Expression</returns>
  94. protected override Expression VisitParameter(ParameterExpression p)
  95. {
  96. ParameterExpression replacement;
  97. if (map.TryGetValue(p, out replacement))
  98. {
  99. p = replacement;
  100. }
  101. return base.VisitParameter(p);
  102. }
  103. }
  104. }
  105. }