diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs new file mode 100644 index 000000000..50a3d7151 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + internal class QueryableFormat + { + public Type Type { get; set; } + public string TypeString { get; set; } + public string Format { get; set; } + public string PropertyName { get; set; } + public string MethodName { get; set; } + } +} diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index 442fe44c2..37748cbf9 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -1003,6 +1003,8 @@ namespace SqlSugar #endregion #region NoCopy + + internal List QueryableFormats { get; set; } internal bool IsClone { get; set; } public bool NoCheckInclude { get; set; } public virtual bool IsSelectNoAll { get; set; } = false; diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs index f6633f950..ae248757a 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs @@ -18,6 +18,28 @@ namespace SqlSugar parameter.Context.Result.Append(this.Context.GetAsString2(asName, this.Context.DbMehtods.NewUid(null))); return; } + else if (ExpressionTool.GetMethodName(item) == "ToString" + &&(item as MethodCallExpression)?.Arguments?.Count()==1 + && (item as MethodCallExpression)?.Object?.Type!=UtilConstants.DateType + && this.Context?.SugarContext?.QueryBuilder!=null) + { + var format=ExpressionTool.GetExpressionValue((item as MethodCallExpression)?.Arguments[0]); + var childExpression = (item as MethodCallExpression)?.Object; + var type=childExpression.Type; + if (this.Context.SugarContext.QueryBuilder.QueryableFormats == null) + { + this.Context.SugarContext.QueryBuilder.QueryableFormats = new List(); + } + this.Context.SugarContext.QueryBuilder.QueryableFormats.Add(new QueryableFormat() { + Format=format+"", + PropertyName=asName, + Type=type, + TypeString=type.FullName, + MethodName= "ToString" + }); + parameter.Context.Result.Append(this.Context.GetAsString2(asName, GetNewExpressionValue(childExpression))); + return; + } this.Expression = item; this.Start(); if (ExpressionTool.GetMethodName(item) == "MappingColumn") diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs index 521e6a3dd..606bd1077 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs @@ -482,6 +482,12 @@ namespace SqlSugar if (readerValues.Any(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase))) { var addValue = readerValues.ContainsKey(name) ? readerValues[name] : readerValues.First(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Value; + if (addValue!=null&&this.QueryBuilder?.QueryableFormats?.Any(it=>it.PropertyName==name)==true) + { + var valueFomatInfo = this.QueryBuilder?.QueryableFormats?.First(it => it.PropertyName == name); + addValue =UtilMethods.GetFormatValue(addValue,valueFomatInfo); + + } if (addValue == DBNull.Value || addValue == null) { if (item.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.DecType, UtilConstants.DobType, UtilConstants.ByteType)) @@ -524,6 +530,7 @@ namespace SqlSugar return result; } + private void SetAppendColumns(IDataReader dataReader) { diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 7f3ee553b..7d9af4296 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -124,6 +124,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index 399636a26..125a3e697 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -18,6 +18,51 @@ namespace SqlSugar { public class UtilMethods { + /// + /// Available only in Select,Handles logic that cannot be completed by an expression + /// + /// + /// + /// + internal static object GetFormatValue(object addValue, QueryableFormat valueFomatInfo) + { + if (valueFomatInfo.MethodName == "ToString") + { + if (valueFomatInfo.Type == UtilConstants.GuidType) + { + addValue = Guid.Parse(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.ByteType) + { + addValue = Convert.ToByte(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.IntType) + { + addValue = Convert.ToInt32(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.LongType) + { + addValue = Convert.ToInt64(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.UIntType) + { + addValue = Convert.ToUInt32(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.ULongType) + { + addValue = Convert.ToUInt64(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.DecType) + { + addValue = Convert.ToDecimal(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.DobType) + { + addValue = Convert.ToDouble(addValue + "").ToString(valueFomatInfo.Format); + } + } + return addValue; + } public static int CountSubstringOccurrences(string mainString, string searchString) { string[] substrings = mainString.Split(new string[] { searchString }, StringSplitOptions.None);