Extension.Linq.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Extensions
  8. {
  9. public static class LinqExtensions
  10. {
  11. public static Expression Property(this Expression expression, string propertyName)
  12. {
  13. return Expression.Property(expression, propertyName);
  14. }
  15. public static Expression AndAlso(this Expression left, Expression right)
  16. {
  17. return Expression.AndAlso(left, right);
  18. }
  19. public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
  20. {
  21. return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
  22. }
  23. public static Expression GreaterThan(this Expression left, Expression right)
  24. {
  25. return Expression.GreaterThan(left, right);
  26. }
  27. public static Expression<T> ToLambda<T>(this Expression body, params ParameterExpression[] parameters)
  28. {
  29. return Expression.Lambda<T>(body, parameters);
  30. }
  31. public static Expression<Func<T, bool>> True<T>() { return param => true; }
  32. public static Expression<Func<T, bool>> False<T>() { return param => false; }
  33. /// <summary>
  34. /// 组合And
  35. /// </summary>
  36. /// <returns></returns>
  37. public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
  38. {
  39. return first.Compose(second, Expression.AndAlso);
  40. }
  41. /// <summary>
  42. /// 组合Or
  43. /// </summary>
  44. /// <returns></returns>
  45. public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
  46. {
  47. return first.Compose(second, Expression.OrElse);
  48. }
  49. /// <summary>
  50. /// Combines the first expression with the second using the specified merge function.
  51. /// </summary>
  52. static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
  53. {
  54. var map = first.Parameters
  55. .Select((f, i) => new { f, s = second.Parameters[i] })
  56. .ToDictionary(p => p.s, p => p.f);
  57. var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
  58. return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
  59. }
  60. /// <summary>
  61. /// ParameterRebinder
  62. /// </summary>
  63. private class ParameterRebinder : ExpressionVisitor
  64. {
  65. /// <summary>
  66. /// The ParameterExpression map
  67. /// </summary>
  68. readonly Dictionary<ParameterExpression, ParameterExpression> map;
  69. /// <summary>
  70. /// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
  71. /// </summary>
  72. /// <param name="map">The map.</param>
  73. ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
  74. {
  75. this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
  76. }
  77. /// <summary>
  78. /// Replaces the parameters.
  79. /// </summary>
  80. /// <param name="map">The map.</param>
  81. /// <param name="exp">The exp.</param>
  82. /// <returns>Expression</returns>
  83. public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
  84. {
  85. return new ParameterRebinder(map).Visit(exp);
  86. }
  87. /// <summary>
  88. /// Visits the parameter.
  89. /// </summary>
  90. /// <param name="p">The p.</param>
  91. /// <returns>Expression</returns>
  92. protected override Expression VisitParameter(ParameterExpression p)
  93. {
  94. ParameterExpression replacement;
  95. if (map.TryGetValue(p, out replacement))
  96. {
  97. p = replacement;
  98. }
  99. return base.VisitParameter(p);
  100. }
  101. }
  102. }
  103. }