SubSelect.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 SubSelect : ISubOperation
  9. {
  10. public bool HasWhere
  11. {
  12. get; set;
  13. }
  14. public string Name
  15. {
  16. get
  17. {
  18. return "Select";
  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. InitType(exp);
  40. var result = "";
  41. var oldIsSingle = this.Context.IsSingle;
  42. this.Context.IsSingle = false;
  43. result = SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldMultiple);
  44. this.Context.IsSingle = oldIsSingle;
  45. SetShortName(exp, result);
  46. return result;
  47. }
  48. private void InitType(MethodCallExpression exp)
  49. {
  50. foreach (var arg in (exp.Arguments[0] as LambdaExpression).Parameters)
  51. {
  52. if (this.Context.InitMappingInfo != null)
  53. {
  54. this.Context.InitMappingInfo(arg.Type);
  55. this.Context.RefreshMapping();
  56. }
  57. }
  58. }
  59. public void SetShortName(MethodCallExpression exp, string result)
  60. {
  61. if (exp.Arguments[0] is LambdaExpression)
  62. {
  63. var parameters = (exp.Arguments[0] as LambdaExpression).Parameters;
  64. if (parameters != null && parameters.Count > 0)
  65. {
  66. this.Context.CurrentShortName = this.Context.GetTranslationColumnName(parameters[0].ObjToString());
  67. }
  68. }
  69. }
  70. public void SetShortNameNext(MethodCallExpression exp, string result)
  71. {
  72. if (exp.Arguments.Count>1&&exp.Arguments[1] is LambdaExpression )
  73. {
  74. var parameters = (exp.Arguments[1] as LambdaExpression).Parameters;
  75. if (parameters != null && parameters.Count > 0)
  76. {
  77. this.Context.CurrentShortName = this.Context.GetTranslationColumnName(parameters[0].ObjToString());
  78. }
  79. }
  80. }
  81. }
  82. }