diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/FilterProvider/FilterProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/FilterProvider/FilterProvider.cs index 4ce1b8d8e..b18524b4c 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/FilterProvider/FilterProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/FilterProvider/FilterProvider.cs @@ -120,6 +120,11 @@ namespace SqlSugar } return this; } + public QueryFilterProvider AddTableFilter(Type type,string shortName, FormattableString expString, FilterJoinPosition filterJoinType = FilterJoinPosition.On) + { + var exp = DynamicCoreHelper.GetWhere(type, shortName, expString); + return AddTableFilter(type, exp, filterJoinType); + } public QueryFilterProvider AddTableFilter(Type type,Expression expression, FilterJoinPosition filterJoinType = FilterJoinPosition.On) { var isOn = filterJoinType == FilterJoinPosition.On; diff --git a/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs b/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs index a42aaecb0..c8b24aa5e 100644 --- a/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs +++ b/Src/Asp.NetCore2/SqlSugar/Infrastructure/StaticConfig.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Text; namespace SqlSugar @@ -26,5 +27,6 @@ namespace SqlSugar public static bool Check_StringIdentity = true; public static Func Check_FieldFunc; + public static Type DynamicExpressionParserType; } } diff --git a/Src/Asp.NetCore2/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs b/Src/Asp.NetCore2/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs new file mode 100644 index 000000000..e70917936 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Text; +using System.Text.RegularExpressions; + +namespace SqlSugar +{ + public class DynamicCoreHelper + { + public static LambdaExpression GetWhere(Type entityType, string shortName, FormattableString whereSql) + { + var parameter = Expression.Parameter(entityType, "it"); + + // 提取 FormattableString 中的参数值 + var arguments = whereSql.GetArguments(); + + + var sql = ReplaceFormatParameters(whereSql.Format); + + // 构建动态表达式,使用常量表达式和 whereSql 中的参数值 + var lambda = SqlSugarDynamicExpressionParser.ParseLambda( + new[] { parameter }, + typeof(bool), + sql, + whereSql.GetArguments() + ); + + return lambda; + } + private static string ReplaceFormatParameters(string format) + { + int parameterIndex = 0; // 起始参数索引 + return Regex.Replace(format, @"\{\d+\}", match => + { + string replacement = $"@{parameterIndex}"; + parameterIndex++; + return replacement; + }); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/Json2Sql/DynamicLinq/SqlSugarDynamicExpressionParser.cs b/Src/Asp.NetCore2/SqlSugar/Json2Sql/DynamicLinq/SqlSugarDynamicExpressionParser.cs new file mode 100644 index 000000000..88393cdee --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Json2Sql/DynamicLinq/SqlSugarDynamicExpressionParser.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; + +namespace SqlSugar +{ + public class SqlSugarDynamicExpressionParser + { + + public static LambdaExpression ParseLambda(ParameterExpression[] parameterExpressions, Type type, string sql, object[] objects) + { + if (StaticConfig.DynamicExpressionParserType == null) + { + Check.ExceptionEasy("Please at program startup assignment: StaticConfig DynamicExpressionParserType = typeof (DynamicExpressionParser); NUGET is required to install Dynamic.Core", "请在程序启动时赋值: StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser); 需要NUGET安装 Dynamic.Core"); + } + + // 查找 ParseLambda 方法 + MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType + .GetMyMethod("ParseLambda",4, typeof(ParameterExpression[]), typeof(Type), typeof(string), typeof(object[])); + + if (parseLambdaMethod == null) + { + throw new InvalidOperationException("ParseLambda method not found in DynamicExpressionParserType."); + } + + // 调用 ParseLambda 方法来解析 Lambda 表达式 + var lambda = (LambdaExpression)parseLambdaMethod.Invoke(null, new object[] { parameterExpressions, type, sql, objects }); + + return lambda; + } + + } +} +