diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs index 50a3d7151..a2f71ef32 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -13,5 +14,6 @@ namespace SqlSugar public string Format { get; set; } public string PropertyName { get; set; } public string MethodName { get; set; } + public MethodInfo MethodInfo { get; set; } } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index e6938665d..1e6e969bc 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -392,5 +393,10 @@ namespace SqlSugar { throw new NotSupportedException("Can only be used in expressions"); } + + public static string OnlyInSelectConvertToString(string stringValue, MethodInfo methodInfo) + { + throw new NotSupportedException("Can only be used in expressions"); + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs index 0a19e5f1c..fe0195a7e 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs @@ -263,5 +263,20 @@ namespace SqlSugar this.Context.Result.Replace(ExpressionConst.FormatSymbol, "-"); } } + private void AppendOnlyInSelectConvertToString(ExpressionParameter parameter, Expression item, string asName) + { + var name =GetNewExpressionValue((item as MethodCallExpression)?.Arguments[0]); + var methodInfo = ExpressionTool.DynamicInvoke(((item as MethodCallExpression)?.Arguments[1])); + if (this.Context.SugarContext.QueryBuilder.QueryableFormats == null) + this.Context.SugarContext.QueryBuilder.QueryableFormats = new List(); + this.Context.SugarContext.QueryBuilder.QueryableFormats.Add(new QueryableFormat() + { + Format = "", + PropertyName = asName, + MethodName = "OnlyInSelectConvertToString", + MethodInfo= (MethodInfo)methodInfo + }); + parameter.Context.Result.Append(this.Context.GetAsString2(asName, name)); + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs index 6affff36f..19cad43c8 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs @@ -18,6 +18,11 @@ namespace SqlSugar parameter.Context.Result.Append(this.Context.GetAsString2(asName, this.Context.DbMehtods.NewUid(null))); return; } + else if (ExpressionTool.GetMethodName(item) == "OnlyInSelectConvertToString") + { + AppendOnlyInSelectConvertToString(parameter, item, asName); + return; + } else if (ExpressionTool.GetMethodName(item) == "ToString" &&(item as MethodCallExpression)?.Arguments?.Count()==1 && (item as MethodCallExpression)?.Object?.Type!=UtilConstants.DateType diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec b/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec index 47634911a..a374ba50d 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec @@ -2,7 +2,7 @@ SqlSugarCore - 5.1.4.111 + 5.1.4.113-preview28 sunkaixuan 果糖大数据科技 http://www.apache.org/licenses/LICENSE-2.0.html diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs index db5f4aba6..2f684116b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -135,9 +135,25 @@ namespace SqlSugar { addValue = Convert.ToDouble(addValue + "").ToString(valueFomatInfo.Format); } - else if (valueFomatInfo.TypeString == "Enum") + else if (valueFomatInfo.TypeString == "Enum") { - addValue =ChangeType2( addValue,valueFomatInfo.Type)?.ToString(); + addValue =ChangeType2(addValue, valueFomatInfo.Type)?.ToString(); + } + } + else if (valueFomatInfo.MethodName== "OnlyInSelectConvertToString") + { + + var methodInfo = valueFomatInfo.MethodInfo; + if (methodInfo != null) + { + // 如果方法是静态的,传递null作为第一个参数,否则传递类的实例 + object instance = methodInfo.IsStatic ? null : Activator.CreateInstance(methodInfo.ReflectedType); + + // 创建一个包含参数值的object数组 + object[] parameters = new object[] { addValue }; + + // 调用方法 + addValue=methodInfo.Invoke(instance, parameters); } } return addValue;