diff --git a/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs b/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs index 8107f72c6..490f11028 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/8_JoinSql.cs @@ -21,8 +21,10 @@ namespace OrmTest.Demo ConditionalModel(); JoinExp(); Clone(); + WhereClassTest(); } + private static void Clone() { var db = GetInstance(); @@ -119,5 +121,40 @@ namespace OrmTest.Demo Console.WriteLine(ex.Message); } } + + private static void WhereClassTest() + { + var db = GetInstance(); + var list=db.Queryable().WhereClass(new WhereClass() { Id=1 }).ToList(); + //where id=1 + var list2 = db.Queryable().WhereClass(new WhereClass() { Name="a"},ignoreDefaultValue:true).ToList(); + //where name="a" + var list3 = db.Queryable().WhereClass(new WhereClass() { Name = "a" }).ToList(); + //where id=0 and name="a" + var list4 = db.Queryable().WhereClass(new WhereClass() { SchoolId="1", Name = "a" },ignoreDefaultValue:true).ToList(); + //school=1,name=a + var list5= db.Queryable().WhereClass(new WhereClass() { SchoolId = "1", Name = "a" }).ToList(); + //school=1,name=a,id=0 + + var list6 = db.Queryable().WhereClass(new List() { + new WhereClass(){ Name="a",SchoolId="1" }, + new WhereClass(){ Id=1 } + },ignoreDefaultValue:true).ToList(); + //(name=a and schoolid=1) or id=1 + + var list7 = db.Queryable().WhereClass(new List() { + new WhereClass(){ Name="a",SchoolId="1" }, + new WhereClass(){ Id=1 } + }).ToList(); + //(name=a and schoolid=1 and id=0) or id=1 + } + + public class WhereClass{ + + public string Name { get; set; } + public int Id { get; set; } + public string SchoolId { get; set; } + + } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 2d73f488d..f5d2607e9 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -97,7 +97,7 @@ namespace SqlSugar { return _Mapper(mapperObject, mapperField); } - + public virtual ISugarQueryable AddParameters(object parameters) { if (parameters != null) @@ -138,6 +138,70 @@ namespace SqlSugar }); return this; } + + /// + /// if a property that is not empty is a condition + /// + /// + /// + public ISugarQueryable WhereClass(ClassType whereClass, bool ignoreDefaultValue = false) where ClassType : class, new() + { + return WhereClass(new List() { whereClass },ignoreDefaultValue); + } + /// + /// if a property that is not empty is a condition + /// + /// + /// + public ISugarQueryable WhereClass(List whereClassTypes, bool ignoreDefaultValue = false) where ClassType : class, new() + { + + if (whereClassTypes.HasValue()) + { + var columns = this.Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => it.IsIgnore == false).ToList(); + List whereModels = new List(); + foreach (var item in whereClassTypes) + { + var cons = new ConditionalCollections(); + foreach (var column in columns) + { + + var value = column.PropertyInfo.GetValue(item, null); + WhereType WhereType = WhereType.And; + var isNotNull = ignoreDefaultValue == false&&value != null ; + var isNotNullAndDefault = ignoreDefaultValue&& value!=null && value.ObjToString() != UtilMethods.DefaultForType(column.PropertyInfo.PropertyType).ObjToString(); + if (isNotNull||isNotNullAndDefault) + { + if (cons.ConditionalList == null) + { + cons.ConditionalList = new List>(); + if (QueryBuilder.WhereInfos.IsNullOrEmpty() && whereModels.IsNullOrEmpty()) + { + + } + else + { + WhereType = WhereType.Or; + } + + } + cons.ConditionalList.Add(new KeyValuePair(WhereType, new ConditionalModel() + { + ConditionalType = ConditionalType.Equal, + FieldName = column.DbColumnName, + FieldValue = value.ObjToString() + })); + } + } + if (cons.HasValue()) + { + whereModels.Add(cons); + } + } + this.Where(whereModels); + } + return this; + } public virtual ISugarQueryable Where(Expression> expression) { this._Where(expression); @@ -1231,14 +1295,16 @@ namespace SqlSugar { mapperObject = ((mapperObject as LambdaExpression).Body as UnaryExpression).Operand; } - else { - mapperObject= (mapperObject as LambdaExpression).Body; + else + { + mapperObject = (mapperObject as LambdaExpression).Body; } if ((mapperField as LambdaExpression).Body is UnaryExpression) { mapperField = ((mapperField as LambdaExpression).Body as UnaryExpression).Operand; } - else { + else + { mapperField = (mapperField as LambdaExpression).Body; } Check.Exception(mapperObject is MemberExpression == false || mapperField is MemberExpression == false, ".Mapper() parameter error"); @@ -1247,8 +1313,9 @@ namespace SqlSugar Check.Exception(mapperFieldExp.Type.IsClass(), ".Mapper() parameter error"); var objType = mapperObjectExp.Type; var filedType = mapperFieldExp.Expression.Type; - Check.Exception(objType != typeof(TObject)&& objType != typeof(List), ".Mapper() parameter error"); - if (objType == typeof(List)){ + Check.Exception(objType != typeof(TObject) && objType != typeof(List), ".Mapper() parameter error"); + if (objType == typeof(List)) + { objType = typeof(TObject); } var filedName = mapperFieldExp.Member.Name; @@ -1286,7 +1353,7 @@ namespace SqlSugar var list = this.Context.Queryable().Where(wheres).ToList(); foreach (var item in entitys) { - var whereValue = item.GetType().GetProperty(filedName).GetValue(item,null); + var whereValue = item.GetType().GetProperty(filedName).GetValue(item, null); var setValue = list.Where(x => x.GetType().GetProperty(whereCol.PropertyName).GetValue(x, null).ObjToString() == whereValue.ObjToString()).ToList(); var setObject = item.GetType().GetProperty(objName); if (setObject.PropertyType.FullName.IsCollectionsList()) @@ -2971,43 +3038,43 @@ namespace SqlSugar _OrderBy(expression, type); return this; } - public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) + public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) { if (isOrderBy) base.OrderBy(orderFileds); return this; } - public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); @@ -3380,49 +3447,49 @@ namespace SqlSugar _GroupBy(expression); return this; } - public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) + public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) { if (isOrderBy) base.OrderBy(orderFileds); return this; } - public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); @@ -3745,55 +3812,55 @@ namespace SqlSugar _OrderBy(expression, type); return this; } - public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) + public new ISugarQueryable OrderByIF(bool isOrderBy, string orderFileds) { if (isOrderBy) base.OrderBy(orderFileds); return this; } - public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public new ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); return this; } - public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) + public ISugarQueryable OrderByIF(bool isOrderBy, Expression> expression, OrderByType type = OrderByType.Asc) { if (isOrderBy) _OrderBy(expression, type); diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs index 5649f2ed7..e00813b54 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs @@ -456,7 +456,11 @@ namespace SqlSugar var index = item.ConditionalList.IndexOf(con); var isFirst = index == 0; var isLast = index == (item.ConditionalList.Count - 1); - if (isFirst) + if (models.IndexOf(item) == 0 &&index==0&& beginIndex == 0) + { + builder.AppendFormat(" ( "); + + }else if (isFirst) { builder.AppendFormat(" {0} ( ", con.Key.ToString().ToUpper()); } diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index f97e67de2..8c5d49585 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -28,6 +28,19 @@ namespace SqlSugar ISugarQueryable AddParameters(List parameters); ISugarQueryable AddJoinInfo(string tableName, string shortName, string joinWhere, JoinType type = JoinType.Left); + /// + /// if a property that is not empty is a condition + /// + /// + /// + ISugarQueryable WhereClass(ClassType whereClass,bool ignoreDefaultValue=false)where ClassType: class,new(); + /// + /// if a property that is not empty is a condition + /// + /// + /// + ISugarQueryable WhereClass(List whereClassTypes,bool ignoreDefaultValue = false) where ClassType : class, new(); + ISugarQueryable Where(Expression> expression); ISugarQueryable Where(string whereString, object parameters = null); ISugarQueryable Where(List conditionalModels);