diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs index b5cd38ef9..9ca8cded1 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs @@ -8,6 +8,12 @@ namespace SqlSugar { public class ExpressionTool { + public static bool IsVariable(Expression expr) + { + var ps = new ParameterExpressionVisitor(); + ps.Visit(expr); + return ps.Parameters.Count==0; + } public static bool IsComparisonOperatorBool(BinaryExpression binaryExp) { return binaryExp.NodeType.IsIn(ExpressionType.Equal, diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ListAnyParameter.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ListAnyParameter.cs new file mode 100644 index 000000000..88a95c1ef --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ListAnyParameter.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + internal class ListAnyParameter + { + public string Name { get; internal set; } + public string Sql { get; internal set; } + public List Columns { get; internal set; } + public Func ConvetColumnFunc { get; internal set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ParameterExpressionVisitor.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ParameterExpressionVisitor.cs new file mode 100644 index 000000000..a7e43d4b5 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Common/ParameterExpressionVisitor.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + internal class ParameterExpressionVisitor : ExpressionVisitor + { + public List Parameters { get; } = new List(); + + protected override Expression VisitParameter(ParameterExpression node) + { + Parameters.Add(node); + return base.VisitParameter(node); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index f164bad08..579191a07 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -732,6 +732,83 @@ namespace SqlSugar }); return $" ({likeString1} or {likeString2} or {likeString3} or {fullString}={value} ) "; } + + + public string ListAny(MethodCallExpressionModel model) + { + StringBuilder sb = new StringBuilder(); + if (model.Args[0].MemberValue!=null&&(model.Args[0].MemberValue as IList).Count>0) + { + sb.Append(" ( "); + var listPar = model.Args[1].MemberValue as ListAnyParameter; + foreach (var item in (model.Args[0].MemberValue as IList)) + { + var sql = listPar.Sql; + if (sb.Length > 3) + { + sb.Append("OR"); + } + foreach (var columnInfo in listPar.Columns) + { + var replace = listPar.ConvetColumnFunc($"{listPar.Name}.{columnInfo.DbColumnName}"); + if(sql.Contains(replace)) + { + var value = columnInfo.PropertyInfo.GetValue(item); + var newValue = "null"; + if (value != null) + { + if (UtilMethods.IsNumber(columnInfo.UnderType.Name)) + { + newValue = value.ToString(); + } + else if(columnInfo.UnderType==SqlSugar.UtilConstants.GuidType) + { + newValue = ToGuid(new MethodCallExpressionModel() + { + Args=new List() + { + new MethodCallExpressionArgs(){ + MemberValue=value.ToSqlValue(), + MemberName=value.ToSqlValue() + } + } + }); + } + else if (columnInfo.UnderType == SqlSugar.UtilConstants.DateType) + { + newValue = ToDate(new MethodCallExpressionModel() + { + Args = new List() + { + new MethodCallExpressionArgs(){ + MemberValue=UtilMethods.GetConvertValue( value).ToSqlValue(), + MemberName=UtilMethods.GetConvertValue( value).ToSqlValue() + } + } + }); + } + else + { + newValue = value.ToSqlValue(); + } + } + sql = sql.Replace(replace, newValue); + } + } + sb.Append(sql); + } + sb.Append(" ) "); + } + var result = sb.ToString(); + if (result.IsNullOrEmpty()) + { + return " 1=2 "; + } + else + { + return result; + } + } public virtual string GetTableWithDataBase(string dataBaseName,string tableName) { return $"{dataBaseName}.{tableName}"; diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index 7f87049fa..c71da6c64 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs @@ -104,6 +104,7 @@ namespace SqlSugar string JsonArrayAny(MethodCallExpressionModel model); string CompareTo(MethodCallExpressionModel model); string SplitIn(MethodCallExpressionModel model); + string ListAny(MethodCallExpressionModel model); string GetTableWithDataBase(string databaseName,string tableName); } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index e7f289f0a..49a471570 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs @@ -284,5 +284,10 @@ namespace SqlSugar { throw new NotSupportedException("Can only be used in expressions"); } + + public static bool ListAny(List listConstant, Expression> expression) + { + throw new NotSupportedException("Can only be used in expressions"); + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index c43fa45d1..f120adf88 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -160,6 +160,10 @@ namespace SqlSugar { var method = express.Method; string name = method.Name; + if (name == "Any" &&ExpressionTool.IsVariable(express.Arguments[0])) + { + name = "ListAny"; + } var args = express.Arguments.Cast().ToList(); MethodCallExpressionModel model = new MethodCallExpressionModel(); model.Args = new List(); diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs index 1c2c80846..704c2e8d9 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve_Helper.cs @@ -252,6 +252,31 @@ namespace SqlSugar } model.Args.Add(argItem); } + else if (name == "ListAny"&& item is LambdaExpression) + { + var sql =GetNewExpressionValue(item,ResolveExpressType.WhereMultiple); + var lamExp = (item as LambdaExpression); + var pExp = lamExp.Parameters[0]; + var pname = pExp.Name; + model.Args.Add(new MethodCallExpressionArgs() { + MemberValue=new ListAnyParameter() { + Sql= sql, + Name=pname, + Columns=this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(pExp.Type).Columns, + ConvetColumnFunc = this.Context.GetTranslationColumnName + } + }); + if (this.Context.IsSingle && this.Context.SingleTableNameSubqueryShortName == null) + { + ParameterExpressionVisitor visitor = new ParameterExpressionVisitor(); + visitor.Visit(lamExp); + var tableParamter=visitor.Parameters.FirstOrDefault(it => it.Name != pname); + if (tableParamter != null) + { + this.Context.SingleTableNameSubqueryShortName = tableParamter.Name; + } + } + } else { AppendModel(parameter, model, item); @@ -792,6 +817,9 @@ namespace SqlSugar return this.Context.DbMehtods.CompareTo(model); case "SplitIn": return this.Context.DbMehtods.SplitIn(model); + case "ListAny": + this.Context.Parameters.RemoveAll(it => model.Args[0].MemberName.ObjToString().Contains(it.ParameterName)); + return this.Context.DbMehtods.ListAny(model); default: break; } @@ -830,6 +858,10 @@ namespace SqlSugar { return true; } + if (expression.Method.Name == "Any"&& ExpressionTool.IsVariable(expression.Arguments[0]) ) + { + return true; + } if (expression.Method.Name == "Format" && expression.Method.DeclaringType == UtilConstants.StringType) { return true;