PredicateBuilder.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using JetBrains.Annotations;
  2. using System.Collections.ObjectModel;
  3. using System.Linq.Expressions;
  4. using System.Runtime.CompilerServices;
  5. namespace VberZero.Tools;
  6. // Codes below are taken from https://github.com/scottksmith95/LINQKit project.
  7. /// <summary> The Predicate Operator </summary>
  8. public enum PredicateOperator
  9. {
  10. /// <summary> The "Or" </summary>
  11. Or,
  12. /// <summary> The "And" </summary>
  13. And
  14. }
  15. /// <summary>
  16. /// See http://www.albahari.com/expressions for information and examples.
  17. /// </summary>
  18. public static class PredicateBuilder
  19. {
  20. private class RebindParameterVisitor : ExpressionVisitor
  21. {
  22. private readonly ParameterExpression _oldParameter;
  23. private readonly ParameterExpression _newParameter;
  24. public RebindParameterVisitor(ParameterExpression oldParameter, ParameterExpression newParameter)
  25. {
  26. _oldParameter = oldParameter;
  27. _newParameter = newParameter;
  28. }
  29. protected override Expression VisitParameter(ParameterExpression node)
  30. {
  31. if (node == _oldParameter)
  32. {
  33. return _newParameter;
  34. }
  35. return base.VisitParameter(node);
  36. }
  37. }
  38. /// <summary> Start an expression </summary>
  39. public static ExpressionStarter<T> New<T>(Expression<Func<T, bool>>? expr = null)
  40. { return new ExpressionStarter<T>(expr); }
  41. /// <summary> Create an expression with a stub expression true or false to use when the expression is not yet started. </summary>
  42. public static ExpressionStarter<T> New<T>(bool defaultExpression)
  43. { return new ExpressionStarter<T>(defaultExpression); }
  44. /// <summary> Always true </summary>
  45. [Obsolete("Use PredicateBuilder.New() instead.")]
  46. public static Expression<Func<T, bool>> True<T>()
  47. { return new ExpressionStarter<T>(true); }
  48. /// <summary> Always false </summary>
  49. [Obsolete("Use PredicateBuilder.New() instead.")]
  50. public static Expression<Func<T, bool>> False<T>()
  51. { return new ExpressionStarter<T>(false); }
  52. /// <summary> OR </summary>
  53. public static Expression<Func<T, bool>> Or<T>([NotNull] this Expression<Func<T, bool>> expr1, [NotNull] Expression<Func<T, bool>> expr2)
  54. {
  55. var expr2Body = new RebindParameterVisitor(expr2.Parameters[0], expr1.Parameters[0]).Visit(expr2.Body);
  56. return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, expr2Body), expr1.Parameters);
  57. }
  58. /// <summary> AND </summary>
  59. public static Expression<Func<T, bool>> And<T>([NotNull] this Expression<Func<T, bool>> expr1, [NotNull] Expression<Func<T, bool>> expr2)
  60. {
  61. var expr2Body = new RebindParameterVisitor(expr2.Parameters[0], expr1.Parameters[0]).Visit(expr2.Body);
  62. return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, expr2Body), expr1.Parameters);
  63. }
  64. /// <summary>
  65. /// Extends the specified source Predicate with another Predicate and the specified PredicateOperator.
  66. /// </summary>
  67. /// <typeparam name="T">The type</typeparam>
  68. /// <param name="first">The source Predicate.</param>
  69. /// <param name="second">The second Predicate.</param>
  70. /// <param name="operator">The Operator (can be "And" or "Or").</param>
  71. /// <returns>Expression{Func{T, bool}}</returns>
  72. public static Expression<Func<T, bool>> Extend<T>([NotNull] this Expression<Func<T, bool>> first, [NotNull] Expression<Func<T, bool>> second, PredicateOperator @operator = PredicateOperator.Or)
  73. {
  74. return @operator == PredicateOperator.Or ? first.Or(second) : first.And(second);
  75. }
  76. /// <summary>
  77. /// Extends the specified source Predicate with another Predicate and the specified PredicateOperator.
  78. /// </summary>
  79. /// <typeparam name="T">The type</typeparam>
  80. /// <param name="first">The source Predicate.</param>
  81. /// <param name="second">The second Predicate.</param>
  82. /// <param name="operator">The Operator (can be "And" or "Or").</param>
  83. /// <returns>Expression{Func{T, bool}}</returns>
  84. public static Expression<Func<T, bool>> Extend<T>([NotNull] this ExpressionStarter<T> first, [NotNull] Expression<Func<T, bool>> second, PredicateOperator @operator = PredicateOperator.Or)
  85. {
  86. return @operator == PredicateOperator.Or ? first.Or(second) : first.And(second);
  87. }
  88. }
  89. /// <summary>
  90. /// ExpressionStarter{T} which eliminates the default 1=0 or 1=1 stub expressions
  91. /// </summary>
  92. /// <typeparam name="T">The type</typeparam>
  93. public class ExpressionStarter<T>
  94. {
  95. internal ExpressionStarter() : this(false)
  96. {
  97. }
  98. internal ExpressionStarter(bool defaultExpression)
  99. {
  100. if (defaultExpression)
  101. DefaultExpression = f => true;
  102. else
  103. DefaultExpression = f => false;
  104. }
  105. internal ExpressionStarter(Expression<Func<T, bool>> exp) : this(false)
  106. {
  107. _predicate = exp;
  108. }
  109. /// <summary>The actual Predicate. It can only be set by calling Start.</summary>
  110. private Expression<Func<T, bool>> Predicate => (IsStarted || !UseDefaultExpression) ? _predicate : DefaultExpression;
  111. private Expression<Func<T, bool>> _predicate;
  112. /// <summary>Determines if the predicate is started.</summary>
  113. public bool IsStarted => _predicate != null;
  114. /// <summary> A default expression to use only when the expression is null </summary>
  115. public bool UseDefaultExpression => DefaultExpression != null;
  116. /// <summary>The default expression</summary>
  117. public Expression<Func<T, bool>> DefaultExpression { get; set; }
  118. /// <summary>Set the Expression predicate</summary>
  119. /// <param name="exp">The first expression</param>
  120. public Expression<Func<T, bool>> Start(Expression<Func<T, bool>> exp)
  121. {
  122. if (IsStarted)
  123. throw new Exception("Predicate cannot be started again.");
  124. return _predicate = exp;
  125. }
  126. /// <summary>Or</summary>
  127. public Expression<Func<T, bool>> Or([NotNull] Expression<Func<T, bool>> expr2)
  128. {
  129. return (IsStarted) ? _predicate = Predicate.Or(expr2) : Start(expr2);
  130. }
  131. /// <summary>And</summary>
  132. public Expression<Func<T, bool>> And([NotNull] Expression<Func<T, bool>> expr2)
  133. {
  134. return (IsStarted) ? _predicate = Predicate.And(expr2) : Start(expr2);
  135. }
  136. /// <summary> Show predicate string </summary>
  137. public override string ToString()
  138. {
  139. return Predicate == null ? null : Predicate.ToString();
  140. }
  141. #region Implicit Operators
  142. /// <summary>
  143. /// Allows this object to be implicitely converted to an Expression{Func{T, bool}}.
  144. /// </summary>
  145. /// <param name="right"></param>
  146. public static implicit operator Expression<Func<T, bool>>(ExpressionStarter<T> right)
  147. {
  148. return right == null ? null : right.Predicate;
  149. }
  150. /// <summary>
  151. /// Allows this object to be implicitely converted to an Expression{Func{T, bool}}.
  152. /// </summary>
  153. /// <param name="right"></param>
  154. public static implicit operator Func<T, bool>(ExpressionStarter<T> right)
  155. {
  156. return right == null ? null : (right.IsStarted || right.UseDefaultExpression) ? right.Predicate.Compile() : null;
  157. }
  158. /// <summary>
  159. /// Allows this object to be implicitely converted to an Expression{Func{T, bool}}.
  160. /// </summary>
  161. /// <param name="right"></param>
  162. public static implicit operator ExpressionStarter<T>(Expression<Func<T, bool>> right)
  163. {
  164. return right == null ? null : new ExpressionStarter<T>(right);
  165. }
  166. #endregion Implicit Operators
  167. #region Implement Expression<TDelagate> methods and properties
  168. #if !(NET35)
  169. /// <summary></summary>
  170. public Func<T, bool> Compile()
  171. { return Predicate.Compile(); }
  172. #endif
  173. #if !(NET35 || WINDOWS_APP || NETSTANDARD || PORTABLE || PORTABLE40 || UAP)
  174. /// <summary></summary>
  175. public Func<T, bool> Compile(DebugInfoGenerator debugInfoGenerator)
  176. { return Predicate.Compile(debugInfoGenerator); }
  177. /// <summary></summary>
  178. public Expression<Func<T, bool>> Update(Expression body, IEnumerable<ParameterExpression> parameters)
  179. { return Predicate.Update(body, parameters); }
  180. #endif
  181. #endregion Implement Expression<TDelagate> methods and properties
  182. #region Implement LamdaExpression methods and properties
  183. /// <summary></summary>
  184. public Expression Body => Predicate.Body;
  185. /// <summary></summary>
  186. public ExpressionType NodeType => Predicate.NodeType;
  187. /// <summary></summary>
  188. public ReadOnlyCollection<ParameterExpression> Parameters => Predicate.Parameters;
  189. /// <summary></summary>
  190. public Type Type => Predicate.Type;
  191. #if !(NET35)
  192. /// <summary></summary>
  193. public string Name => Predicate.Name;
  194. /// <summary></summary>
  195. public Type ReturnType => Predicate.ReturnType;
  196. /// <summary></summary>
  197. public bool TailCall => Predicate.TailCall;
  198. #endif
  199. #endregion Implement LamdaExpression methods and properties
  200. #region Implement Expression methods and properties
  201. #if !(NET35)
  202. /// <summary></summary>
  203. public virtual bool CanReduce => Predicate.CanReduce;
  204. #endif
  205. #endregion Implement Expression methods and properties
  206. }