You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.8 KiB

2 years ago
  1. using System;
  2. using System.Linq.Expressions;
  3. namespace SqlSugar
  4. {
  5. public class Expressionable<T> where T : class, new()
  6. {
  7. private Expression<Func<T, bool>> _exp = null;
  8. public Expressionable<T> And(Expression<Func<T, bool>> exp)
  9. {
  10. _exp = _exp == null ? exp : Expression.Lambda<Func<T, bool>>(Expression.AndAlso(_exp.Body, exp.Body), _exp.Parameters);
  11. return this;
  12. }
  13. public Expressionable<T> AndIF(bool isAnd, Expression<Func<T, bool>> exp)
  14. {
  15. if (isAnd)
  16. And(exp);
  17. return this;
  18. }
  19. public Expressionable<T> Or(Expression<Func<T, bool>> exp)
  20. {
  21. _exp = _exp == null ? exp : Expression.Lambda<Func<T, bool>>(Expression.OrElse(_exp.Body, exp.Body), _exp.Parameters);
  22. return this;
  23. }
  24. public Expressionable<T> OrIF(bool isOr, Expression<Func<T, bool>> exp)
  25. {
  26. if (isOr)
  27. Or(exp);
  28. return this;
  29. }
  30. public Expression<Func<T, bool>> ToExpression()
  31. {
  32. if (_exp == null)
  33. _exp = it => true;
  34. return _exp;
  35. }
  36. }
  37. public class Expressionable<T, T2> where T : class, new() where T2 : class, new()
  38. {
  39. private Expression<Func<T, T2, bool>> _exp = null;
  40. public Expressionable<T, T2> And(Expression<Func<T, T2, bool>> exp)
  41. {
  42. _exp = _exp == null ? exp : Expression.Lambda<Func<T, T2, bool>>(Expression.AndAlso(_exp.Body, exp.Body), _exp.Parameters);
  43. return this;
  44. }
  45. public Expressionable<T, T2> AndIF(bool isAnd, Expression<Func<T, T2, bool>> exp)
  46. {
  47. if (isAnd)
  48. And(exp);
  49. return this;
  50. }
  51. public Expressionable<T, T2> Or(Expression<Func<T, T2, bool>> exp)
  52. {
  53. _exp = _exp == null ? exp : Expression.Lambda<Func<T, T2, bool>>(Expression.OrElse(_exp.Body, exp.Body), _exp.Parameters);
  54. return this;
  55. }
  56. public Expressionable<T, T2> OrIF(bool isOr, Expression<Func<T, T2, bool>> exp)
  57. {
  58. if (isOr)
  59. Or(exp);
  60. return this;
  61. }
  62. public Expression<Func<T, T2, bool>> ToExpression()
  63. {
  64. if (_exp == null)
  65. _exp = (it, t2) => true;
  66. return _exp;
  67. }
  68. }
  69. public class Expressionable<T, T2, T3> where T : class, new() where T2 : class, new() where T3 : class, new()
  70. {
  71. private Expression<Func<T, T2, T3, bool>> _exp = null;
  72. public Expressionable<T, T2, T3> And(Expression<Func<T, T2, T3, bool>> exp)
  73. {
  74. _exp = _exp == null ? exp : Expression.Lambda<Func<T, T2, T3, bool>>(Expression.AndAlso(_exp.Body, exp.Body), _exp.Parameters);
  75. return this;
  76. }
  77. public Expressionable<T, T2, T3> AndIF(bool isAnd, Expression<Func<T, T2, T3, bool>> exp)
  78. {
  79. if (isAnd)
  80. And(exp);
  81. return this;
  82. }
  83. public Expressionable<T, T2, T3> Or(Expression<Func<T, T2, T3, bool>> exp)
  84. {
  85. _exp = _exp == null ? exp : Expression.Lambda<Func<T, T2, T3, bool>>(Expression.OrElse(_exp.Body, exp.Body), _exp.Parameters);
  86. return this;
  87. }
  88. public Expressionable<T, T2, T3> OrIF(bool isOr, Expression<Func<T, T2, T3, bool>> exp)
  89. {
  90. if (isOr)
  91. Or(exp);
  92. return this;
  93. }
  94. public Expression<Func<T, T2, T3, bool>> ToExpression()
  95. {
  96. if (_exp == null)
  97. _exp = (it, t2, t3) => true;
  98. return _exp;
  99. }
  100. }
  101. public class Expressionable<T, T2, T3, T4> where T : class, new() where T2 : class, new() where T3 : class, new() where T4 : class, new()
  102. {
  103. private Expression<Func<T, T2, T3, T4, bool>> _exp = null;
  104. public Expressionable<T, T2, T3, T4> And(Expression<Func<T, T2, T3, T4, bool>> exp)
  105. {
  106. _exp = _exp == null ? exp : Expression.Lambda<Func<T, T2, T3, T4, bool>>(Expression.AndAlso(_exp.Body, exp.Body), _exp.Parameters);
  107. return this;
  108. }
  109. public Expressionable<T, T2, T3, T4> AndIF(bool isAnd, Expression<Func<T, T2, T3, T4, bool>> exp)
  110. {
  111. if (isAnd)
  112. And(exp);
  113. return this;
  114. }
  115. public Expressionable<T, T2, T3, T4> Or(Expression<Func<T, T2, T3, T4, bool>> exp)
  116. {
  117. _exp = _exp == null ? exp : Expression.Lambda<Func<T, T2, T3, T4, bool>>(Expression.OrElse(_exp.Body, exp.Body), _exp.Parameters);
  118. return this;
  119. }
  120. public Expressionable<T, T2, T3, T4> OrIF(bool isOr, Expression<Func<T, T2, T3, T4, bool>> exp)
  121. {
  122. if (isOr)
  123. Or(exp);
  124. return this;
  125. }
  126. public Expression<Func<T, T2, T3, T4, bool>> ToExpression()
  127. {
  128. if (_exp == null)
  129. _exp = (it, t2, t3, t4) => true;
  130. return _exp;
  131. }
  132. }
  133. public class Expressionable
  134. {
  135. public static Expressionable<T> Create<T>() where T : class, new()
  136. {
  137. return new Expressionable<T>();
  138. }
  139. public static Expressionable<T, T2> Create<T, T2>() where T : class, new() where T2 : class, new()
  140. {
  141. return new Expressionable<T, T2>();
  142. }
  143. public static Expressionable<T, T2, T3> Create<T, T2, T3>() where T : class, new() where T2 : class, new() where T3 : class, new()
  144. {
  145. return new Expressionable<T, T2, T3>();
  146. }
  147. public static Expressionable<T, T2, T3, T4> Create<T, T2, T3, T4>() where T : class, new() where T2 : class, new() where T3 : class, new() where T4 : class, new()
  148. {
  149. return new Expressionable<T, T2, T3, T4>();
  150. }
  151. }
  152. }