SubOrderBy.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 SubOrderBy : ISubOperation
  9. {
  10. public int OrderIndex { get; set; } = 0;
  11. public bool HasWhere
  12. {
  13. get; set;
  14. }
  15. public string Name
  16. {
  17. get { return "OrderBy"; }
  18. }
  19. public Expression Expression
  20. {
  21. get; set;
  22. }
  23. public int Sort
  24. {
  25. get
  26. {
  27. return 480+OrderIndex;
  28. }
  29. }
  30. public ExpressionContext Context
  31. {
  32. get; set;
  33. }
  34. public string GetValue(Expression expression)
  35. {
  36. if (this.Context is OracleExpressionContext)
  37. {
  38. throw new Exception("Oracle Subquery can't OrderBy");
  39. }
  40. var exp = expression as MethodCallExpression;
  41. var argExp = exp.Arguments[0];
  42. var result = "";
  43. if (this.Context.JoinIndex == 0)
  44. {
  45. result = (OrderIndex == 0 ? "ORDER BY " : ",") + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.FieldSingle);
  46. }
  47. else
  48. {
  49. result = (OrderIndex == 0 ? "ORDER BY " : ",") + SubTools.GetMethodValueSubJoin(this.Context, argExp, ResolveExpressType.FieldMultiple);
  50. }
  51. var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
  52. if (this.Context.JoinIndex == 0)
  53. {
  54. result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
  55. }
  56. return result;
  57. }
  58. }
  59. public class SubOrderByDesc : ISubOperation
  60. {
  61. public int OrderIndex { get; set; } = 0;
  62. public bool HasWhere
  63. {
  64. get; set;
  65. }
  66. public string Name
  67. {
  68. get { return "OrderByDesc"; }
  69. }
  70. public Expression Expression
  71. {
  72. get; set;
  73. }
  74. public int Sort
  75. {
  76. get
  77. {
  78. return 480+OrderIndex;
  79. }
  80. }
  81. public ExpressionContext Context
  82. {
  83. get; set;
  84. }
  85. public string GetValue(Expression expression)
  86. {
  87. var exp = expression as MethodCallExpression;
  88. var argExp = exp.Arguments[0];
  89. var result = "";
  90. if (this.Context.JoinIndex == 0)
  91. {
  92. result = (OrderIndex == 0 ? "ORDER BY " : ",") + SubTools.GetMethodValue(this.Context, argExp, ResolveExpressType.FieldSingle) + " DESC";
  93. }
  94. else
  95. {
  96. result = (OrderIndex == 0 ? "ORDER BY " : ",") + SubTools.GetMethodValueSubJoin(this.Context, argExp, ResolveExpressType.FieldMultiple) + " DESC";
  97. }
  98. var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
  99. if (this.Context.JoinIndex == 0)
  100. {
  101. result = result.Replace(selfParameterName, string.Empty);
  102. }
  103. return result;
  104. }
  105. }
  106. }