ExpressionExtensions.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Ropin.Inspection.Repository
  8. {
  9. public static class ExpressionExtensions
  10. {
  11. /// <summary>
  12. /// 添加And条件
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. /// <param name="first"></param>
  16. /// <param name="second"></param>
  17. /// <returns></returns>
  18. public static Expression<Func<T, bool>> And<T>(
  19. this Expression<Func<T, bool>> first,
  20. Expression<Func<T, bool>> second)
  21. {
  22. return first.AndAlso<T>(second, Expression.AndAlso);
  23. }
  24. /// <summary>
  25. /// 添加Or条件
  26. /// </summary>
  27. /// <typeparam name="T"></typeparam>
  28. /// <param name="first"></param>
  29. /// <param name="second"></param>
  30. /// <returns></returns>
  31. public static Expression<Func<T, bool>> Or<T>(
  32. this Expression<Func<T, bool>> first,
  33. Expression<Func<T, bool>> second)
  34. {
  35. return first.AndAlso<T>(second, Expression.OrElse);
  36. }
  37. /// <summary>
  38. /// 合并表达式以及参数
  39. /// </summary>
  40. /// <typeparam name="T"></typeparam>
  41. /// <param name="expr1"></param>
  42. /// <param name="expr2"></param>
  43. /// <param name="func"></param>
  44. /// <returns></returns>
  45. private static Expression<Func<T, bool>> AndAlso<T>(
  46. this Expression<Func<T, bool>> expr1,
  47. Expression<Func<T, bool>> expr2,
  48. Func<Expression, Expression, BinaryExpression> func)
  49. {
  50. var parameter = Expression.Parameter(typeof(T));
  51. var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], parameter);
  52. var left = leftVisitor.Visit(expr1.Body);
  53. var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter);
  54. var right = rightVisitor.Visit(expr2.Body);
  55. return Expression.Lambda<Func<T, bool>>(
  56. func(left, right), parameter);
  57. }
  58. private class ReplaceExpressionVisitor
  59. : ExpressionVisitor
  60. {
  61. private readonly Expression _oldValue;
  62. private readonly Expression _newValue;
  63. public ReplaceExpressionVisitor(Expression oldValue, Expression newValue)
  64. {
  65. _oldValue = oldValue;
  66. _newValue = newValue;
  67. }
  68. public override Expression Visit(Expression node)
  69. {
  70. if (node == _oldValue)
  71. return _newValue;
  72. return base.Visit(node);
  73. }
  74. }
  75. }
  76. // Expression<Func<IdentityUser, bool>> filter = u => true;
  77. // if (!string.IsNullOrEmpty(username))
  78. // {
  79. // filter = filter.And(c => c.UserName.Contains(username));
  80. // }
  81. //if (!string.IsNullOrEmpty(phone))
  82. //{
  83. // filter = filter.And(c => c.PhoneNumber.Contains(phone));
  84. //}
  85. //if (!string.IsNullOrEmpty(email))
  86. //{
  87. // filter = filter.And(c => c.Email.Contains(email));
  88. //}
  89. }