Synchronization code

This commit is contained in:
sunkaixuan
2023-10-29 15:12:14 +08:00
parent 6365e0e846
commit 34b23e423f
6 changed files with 54 additions and 2 deletions

View File

@@ -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; }
}
}

View File

@@ -447,6 +447,14 @@ namespace SqlSugar
var result = this.Select<T>(keyName + "," + valueName).ToList().ToDictionary(ExpressionTool.GetMemberName(key), ExpressionTool.GetMemberName(value));
return result;
}
else if (valueName == null)
{
// 编译key和value的表达式树为委托
var keySelector = key.Compile();
var valueSelector = value.Compile();
Dictionary<object,object> objDic= this.ToList().ToDictionary(keySelector, valueSelector);
return objDic.ToDictionary(it=>it.Key?.ToString(),it=>it.Value);
}
else
{
var result = this.Select<KeyValuePair<string, object>>(keyName + "," + valueName).ToList().ToDictionary(it => it.Key.ObjToString(), it => it.Value);

View File

@@ -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");
}
}
}

View File

@@ -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<QueryableFormat>();
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));
}
}
}

View File

@@ -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

View File

@@ -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;