using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OrmTest { public class Demo1_Queryable { public static void Init() { ConditionScreening(); Async(); } private static void ConditionScreening() { Console.WriteLine(""); Console.WriteLine("#### Condition Screening Start ####"); SqlSugarClient db = GetInstance(); /*** By expression***/ //id=@id var list = db.Queryable().Where(it => it.Id == 1).ToList(); //id=@id or name like '%'+@name+'%' var list2 = db.Queryable().Where(it => it.Id == 1 || it.Name.Contains("jack")).ToList(); //Create expression var exp = Expressionable.Create() .And(it => it.Id == 1) .Or(it => it.Name.Contains("jack")).ToExpression(); var list3 = db.Queryable().Where(exp).ToList(); /*** By sql***/ //id=@id var list4 = db.Queryable().Where("id=@id", new { id = 1 }).ToList(); //id=@id or name like '%'+@name+'%' var list5 = db.Queryable().Where("id=@id or name like '%'+@name+'%' ",new { id=1,name="jack"}).ToList(); /*** By dynamic***/ //id=1 var conModels = new List(); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1 var student = db.Queryable().Where(conModels).ToList(); //Complex use case List Order = new List(); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1 conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" });// id like '%1%' conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNullOrEmpty }); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.In, FieldValue = "1,2,3" }); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NotIn, FieldValue = "1,2,3" }); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1,2,3" }); conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot, FieldValue = null });// id is not null conModels.Add(new ConditionalCollections() { ConditionalList = new List>()// (id=1 or id=2 and id=1) { new KeyValuePair( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }), new KeyValuePair (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }), new KeyValuePair ( WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }) } }); var list6 = db.Queryable().Where(conModels).ToList(); Console.WriteLine("#### Condition Screening End ####"); } private static void Async() { Console.WriteLine(""); Console.WriteLine("#### Async Start ####"); SqlSugarClient db = GetInstance(); var task1 = db.Queryable().FirstAsync(); var task2 = db.Queryable().Where(it => it.Id == 1).ToListAsync(); task1.Wait(); task2.Wait(); Console.WriteLine("#### Async End ####"); } private static SqlSugarClient GetInstance() { return new SqlSugarClient(new ConnectionConfig() { DbType = DbType.SqlServer, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, AopEvents = new AopEvents { OnLogExecuting = (sql, p) => { Console.WriteLine(sql); Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); } } }); } } }