diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryMethodInfo.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryMethodInfo.cs index 648c5319a..f9eff42ff 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryMethodInfo.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryMethodInfo.cs @@ -82,7 +82,12 @@ namespace SqlSugar this.QueryableObj = method.Invoke(QueryableObj, new object[] { groupBySql }); return this; } - + public QueryMethodInfo Where(string expShortName, FormattableString expressionString) + { + var method = QueryableObj.GetType().GetMyMethod("Where", 2, typeof(string),typeof(FormattableString)); + this.QueryableObj = method.Invoke(QueryableObj, new object[] { expShortName, expressionString }); + return this; + } public QueryMethodInfo Where(List conditionalModels) { var method = QueryableObj.GetType().GetMyMethod("Where", 1, typeof(List)); diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 1722f52d8..8d93304cd 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -917,6 +917,12 @@ namespace SqlSugar } return this; } + public virtual ISugarQueryable Where(string expShortName, FormattableString expressionString) + { + var exp = DynamicCoreHelper.GetWhere(expShortName, expressionString); + _Where(exp); + return this; + } public virtual ISugarQueryable Where(Expression> expression) { this._Where(expression); diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/StaticConfig.cs b/Src/Asp.Net/SqlSugar/Infrastructure/StaticConfig.cs index 34fb2bedf..1ef870339 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/StaticConfig.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/StaticConfig.cs @@ -29,5 +29,6 @@ namespace SqlSugar public static bool EnableAllWhereIF = false; public static Func Check_FieldFunc; public static Type DynamicExpressionParserType; + public static object DynamicExpressionParsingConfig; } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 5abe6a59e..ed6ce1665 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -84,6 +84,7 @@ namespace SqlSugar ISugarQueryable WhereColumns(Dictionary columns); ISugarQueryable TranLock(DbLockType? LockType = DbLockType.Wait); ISugarQueryable Where(Expression> expression); + ISugarQueryable Where(string expShortName, FormattableString expressionString); ISugarQueryable Where(string whereString, object parameters = null); ISugarQueryable Where(IFuncModel funcModel); ISugarQueryable Where(List conditionalModels); diff --git a/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs b/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs index e70917936..512ade93c 100644 --- a/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs +++ b/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs @@ -8,6 +8,10 @@ namespace SqlSugar { public class DynamicCoreHelper { + public static Expression> GetWhere(string shortName, FormattableString whereSql) + { + return (Expression>)GetWhere(typeof(T), shortName, whereSql); + } public static LambdaExpression GetWhere(Type entityType, string shortName, FormattableString whereSql) { var parameter = Expression.Parameter(entityType, "it"); diff --git a/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/SqlSugarDynamicExpressionParser.cs b/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/SqlSugarDynamicExpressionParser.cs index 88393cdee..4d306dff8 100644 --- a/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/SqlSugarDynamicExpressionParser.cs +++ b/Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/SqlSugarDynamicExpressionParser.cs @@ -16,19 +16,39 @@ namespace SqlSugar 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) + if (StaticConfig.DynamicExpressionParsingConfig != null) { - throw new InvalidOperationException("ParseLambda method not found in DynamicExpressionParserType."); + // 查找 ParseLambda 方法 + MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType + .GetMyMethod("ParseLambda", 5, StaticConfig.DynamicExpressionParsingConfig.GetType(), 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[] { StaticConfig.DynamicExpressionParsingConfig, parameterExpressions, type, sql, objects }); + + return lambda; } + else + { - // 调用 ParseLambda 方法来解析 Lambda 表达式 - var lambda = (LambdaExpression)parseLambdaMethod.Invoke(null, new object[] { parameterExpressions, type, sql, objects }); + // 查找 ParseLambda 方法 + MethodInfo parseLambdaMethod = StaticConfig.DynamicExpressionParserType + .GetMyMethod("ParseLambda",4, typeof(ParameterExpression[]), typeof(Type), typeof(string), typeof(object[])); - return lambda; + 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; + } } } diff --git a/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs b/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs index 3a855fb28..ceb0602b9 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs @@ -72,6 +72,16 @@ namespace SqlSugar it.GetParameters()[2].ParameterType == parameterType3&& it.GetParameters()[3].ParameterType == parameterType4); } + public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType, Type parameterType2, Type parameterType3, Type parameterType4, Type parameterType5) + { + return type.GetMethods().Where(it => it.Name == name).FirstOrDefault(it => + it.GetParameters().Length == argCount && + it.GetParameters().First().ParameterType == parameterType && + it.GetParameters()[1].ParameterType == parameterType2 && + it.GetParameters()[2].ParameterType == parameterType3 && + it.GetParameters()[3].ParameterType == parameterType4&& + it.GetParameters()[4].ParameterType == parameterType5); + } public static List ToList(this T thisValue,Func action) where T:class,new() { return new List { thisValue };