SubGroupBy.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 SubGroupBy : ISubOperation
  9. {
  10. public bool HasWhere
  11. {
  12. get; set;
  13. }
  14. public string Name
  15. {
  16. get { return "GroupBy"; }
  17. }
  18. public Expression Expression
  19. {
  20. get; set;
  21. }
  22. public int Sort
  23. {
  24. get
  25. {
  26. return 479;
  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. var argExp = exp.Arguments[0];
  37. var type = ResolveExpressType.FieldSingle;
  38. if ((argExp as LambdaExpression).Body is NewExpression) {
  39. type = ResolveExpressType.ArraySingle;
  40. }
  41. var result = "GROUP BY ";
  42. if (this.Context.JoinIndex == 0)
  43. {
  44. result = result + SubTools.GetMethodValue(this.Context, argExp, type);
  45. }
  46. else
  47. {
  48. if (type == ResolveExpressType.ArraySingle)
  49. {
  50. type= ResolveExpressType.ArrayMultiple;
  51. }
  52. else if (type == ResolveExpressType.FieldSingle)
  53. {
  54. type = ResolveExpressType.FieldMultiple;
  55. }
  56. else if (type == ResolveExpressType.WhereSingle)
  57. {
  58. type = ResolveExpressType.WhereMultiple;
  59. }
  60. result = result + SubTools.GetMethodValueSubJoin(this.Context, argExp, type);
  61. }
  62. result = result.TrimEnd(',');
  63. var selfParameterName = this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
  64. if (this.Context.JoinIndex == 0)
  65. result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
  66. if (this.Context.CurrentShortName == null)
  67. {
  68. this.Context.CurrentShortName =this.Context.GetTranslationColumnName(ExpressionTool.GetParameters(exp).FirstOrDefault().Name);
  69. }
  70. return result;
  71. }
  72. }
  73. }