SubMax.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 SubMax:ISubOperation
  9. {
  10. public bool HasWhere
  11. {
  12. get; set;
  13. }
  14. public string Name
  15. {
  16. get
  17. {
  18. return "Max";
  19. }
  20. }
  21. public Expression Expression
  22. {
  23. get; set;
  24. }
  25. public int Sort
  26. {
  27. get
  28. {
  29. return 200;
  30. }
  31. }
  32. public ExpressionContext Context
  33. {
  34. get; set;
  35. }
  36. public string GetValue(Expression expression = null)
  37. {
  38. var exp = expression as MethodCallExpression;
  39. var argExp = exp.Arguments[0];
  40. var parametres = (argExp as LambdaExpression).Parameters;
  41. if ((argExp as LambdaExpression).Body is UnaryExpression)
  42. {
  43. argExp = ((argExp as LambdaExpression).Body as UnaryExpression).Operand;
  44. }
  45. var argLambda = argExp as LambdaExpression;
  46. if (this.Context.InitMappingInfo != null && argLambda != null && argLambda.Parameters.Count > 0)
  47. {
  48. foreach (var item in argLambda.Parameters)
  49. {
  50. this.Context.InitMappingInfo(item.Type);
  51. }
  52. this.Context.RefreshMapping();
  53. }
  54. var result = "MAX(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")";
  55. var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot;
  56. if (this.Context.JoinIndex == 0)
  57. result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
  58. return result;
  59. }
  60. }
  61. }