SubAndIF.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. namespace SqlSugar
  7. {
  8. public class SubAndIF : ISubOperation
  9. {
  10. public bool HasWhere
  11. {
  12. get; set;
  13. }
  14. public string Name
  15. {
  16. get { return "WhereIF"; }
  17. }
  18. public Expression Expression
  19. {
  20. get; set;
  21. }
  22. public int Sort
  23. {
  24. get
  25. {
  26. return 400;
  27. }
  28. }
  29. public ExpressionContext Context
  30. {
  31. get; set;
  32. }
  33. public string GetValue(Expression expression)
  34. {
  35. var exp = expression as MethodCallExpression;
  36. object value = null;
  37. try
  38. {
  39. value = ExpressionTool.DynamicInvoke(exp.Arguments[0]);
  40. }
  41. catch
  42. {
  43. Check.Exception(true, ErrorMessage.WhereIFCheck,exp.Arguments[0].ToString());
  44. }
  45. var isWhere= Convert.ToBoolean(value);
  46. if (!Convert.ToBoolean(isWhere)) {
  47. return "";
  48. }
  49. var argExp = exp.Arguments[1];
  50. var copyContext = this.Context;
  51. if (this.Context.JoinIndex > 0)
  52. {
  53. copyContext = this.Context.GetCopyContextWithMapping();
  54. copyContext.IsSingle = false;
  55. }
  56. var result = "AND " + SubTools.GetMethodValue(copyContext, argExp, ResolveExpressType.WhereMultiple); ;
  57. if (this.Context.JoinIndex > 0)
  58. {
  59. this.Context.Parameters.AddRange(copyContext.Parameters);
  60. this.Context.Index = copyContext.Index;
  61. this.Context.ParameterIndex = copyContext.ParameterIndex;
  62. }
  63. var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
  64. if (this.Context.JoinIndex==0)
  65. {
  66. result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
  67. }
  68. return result;
  69. }
  70. }
  71. }