PredicateExtensions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Threading.Tasks;
  6. namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
  7. {
  8. /// <summary>
  9. //
  10. /*
  11. Expression<Func<UserInfo, bool>> exp = x => x.Status == (int)BizConst.UserStatus.Active;
  12. if(parameters.IsAuthenticated)
  13. {
  14. exp = exp.And(x => x.IdentityAuth == (int) BizConst.IdentityAuthStatus.Authed && x.CareerAuth == (int) BizConst.GlobalCareerStatus.Authed);
  15. }
  16. if(parameters.IsVip)
  17. {
  18. exp = exp.And(x => x.Vip != (int) BizConst.VipTag.NoVip);
  19. }*/
  20. /// </summary>
  21. public static class PredicateExtensions
  22. {
  23. ///// <summary>
  24. ///// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效。即,设置为True时所有or语句应该放在and语句之后,否则无效
  25. ///// </summary>
  26. //public static Expression<Func<T, bool>> True<T>() { return f => true; }
  27. ///// <summary>
  28. ///// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效。 即,设置为False时所有or语句应该放在and语句之前,否则无效
  29. ///// </summary>
  30. //public static Expression<Func<T, bool>> False<T>() { return f => false; }
  31. //public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1,
  32. // Expression<Func<T, bool>> expression2)
  33. //{
  34. // var invokedExpression = Expression.Invoke(expression2, expression1.Parameters
  35. // .Cast<Expression>());
  36. // return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression),
  37. // expression1.Parameters);
  38. //}
  39. //public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,
  40. // Expression<Func<T, bool>> expression2)
  41. //{
  42. // var invokedExpression = Expression.Invoke(expression2, expression1.Parameters
  43. // .Cast<Expression>());
  44. // return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body,
  45. // invokedExpression), expression1.Parameters);
  46. //}
  47. /// <summary>
  48. /// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效。即,设置为True时所有or语句应该放在and语句之后,否则无效
  49. /// </summary>
  50. public static Expression<Func<T, bool>> BaseAnd<T>() { return f => true; }
  51. /// <summary>
  52. /// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效。 即,设置为False时所有or语句应该放在and语句之前,否则无效
  53. /// </summary>
  54. public static Expression<Func<T, bool>> BaseOr<T>() { return f => false; }
  55. public static Expression<Func<T, bool>> Or<T>(
  56. this Expression<Func<T, bool>> expr1,
  57. Expression<Func<T, bool>> expr2)
  58. {
  59. var secondBody = expr2.Body.Replace(expr2.Parameters[0], expr1.Parameters[0]);
  60. return Expression.Lambda<Func<T, bool>>
  61. (Expression.OrElse(expr1.Body, secondBody), expr1.Parameters);
  62. }
  63. public static Expression<Func<T, bool>> And<T>(
  64. this Expression<Func<T, bool>> expr1,
  65. Expression<Func<T, bool>> expr2)
  66. {
  67. var secondBody = expr2.Body.Replace(expr2.Parameters[0], expr1.Parameters[0]);
  68. return Expression.Lambda<Func<T, bool>>
  69. (Expression.AndAlso(expr1.Body, secondBody), expr1.Parameters);
  70. }
  71. public static Expression Replace(this Expression expression,
  72. Expression searchEx, Expression replaceEx)
  73. {
  74. return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
  75. }
  76. public static Expression<Func<T, bool>> CombineOrPreicatesWithAndPredicates<T>(this Expression<Func<T, bool>> combinedPredicate,
  77. Expression<Func<T, bool>> andPredicate, Expression<Func<T, bool>> orPredicate)
  78. {
  79. combinedPredicate = combinedPredicate ?? BaseAnd<T>();
  80. if (andPredicate != null && orPredicate != null)
  81. {
  82. andPredicate = andPredicate.And(orPredicate);
  83. combinedPredicate = combinedPredicate.And(andPredicate);
  84. }
  85. else if (orPredicate != null)
  86. {
  87. combinedPredicate = combinedPredicate.And(orPredicate);
  88. }
  89. else
  90. {
  91. combinedPredicate = combinedPredicate.And(andPredicate);
  92. }
  93. return combinedPredicate;
  94. }
  95. public static void AddToPredicateTypeBasedOnIfAndOrOr<T>(ref Expression<Func<T, bool>> andPredicate,
  96. ref Expression<Func<T, bool>> orPredicate, Expression<Func<T, bool>> newExpression, bool isAnd)
  97. {
  98. if (isAnd)
  99. {
  100. andPredicate = andPredicate ?? BaseAnd<T>();
  101. andPredicate = andPredicate.And(newExpression);
  102. }
  103. else
  104. {
  105. orPredicate = orPredicate ?? BaseOr<T>();
  106. orPredicate = orPredicate.Or(newExpression);
  107. }
  108. }
  109. }
  110. internal class ReplaceVisitor : ExpressionVisitor
  111. {
  112. private readonly Expression from, to;
  113. public ReplaceVisitor(Expression from, Expression to)
  114. {
  115. this.from = from;
  116. this.to = to;
  117. }
  118. public override Expression Visit(Expression node)
  119. {
  120. return node == from ? to : base.Visit(node);
  121. }
  122. }
  123. }