# SqlSugar 4.X ## 1. Query ### 1.1 Create Connection ```c SqlSugarClient db = new SqlSugarClient(new SystemTableConfig() { ConnectionString = Config.ConnectionString, DbType =DbType.SqlServer, IsAutoCloseConnection = true }); ``` ### 1.2 Introduction ```c var getAll = db.Queryable().ToList(); var getAllNoLock = db.Queryable().With(SqlWith.NoLock).ToList(); var getByPrimaryKey = db.Queryable().InSingle(2); var getByWhere = db.Queryable().Where(it => it.Id == 1 || it.Name == "a").ToList(); var getByFuns = db.Queryable().Where(it => NBORM.IsNullOrEmpty(it.Name)).ToList(); ``` ### 1.3 Page ```c var pageIndex = 1; var pageSize = 2; var totalCount = 0; //page var page = db.Queryable().ToPageList(pageIndex, pageSize, ref totalCount); //page join var pageJoin = db.Queryable((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id }).ToPageList(pageIndex, pageSize, ref totalCount); //top 5 var top5 = db.Queryable().Take(5).ToList(); //skip5 var skip5 = db.Queryable().Skip(5).ToList(); ``` ### 1.4 Join ```c //join 2 var list = db.Queryable((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id }).ToList(); //join 3 var list2 = db.Queryable((st, sc,st2) => new object[] { JoinType.Left,st.SchoolId==sc.Id, JoinType.Left,st.SchoolId==st2.Id }).ToList(); //join return List var list3 = db.Queryable((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id }).Select((st,sc)=>new ViewModelStudent { Name= st.Name,SchoolId=sc.Id }).ToList(); //join Order By (order by st.id desc,sc.id desc) var list4 = db.Queryable((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id }) .OrderBy(st=>st.Id,OrderByType.Desc) .OrderBy(sc=>sc.Id,OrderByType.Desc) .Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList(); ``` ### 1.5 SqlFunctions ```c var t1 = db.Queryable().Where(it => NBORM.ToLower(it.Name) == NBORM.ToLower("JACK")).ToList(); //SELECT [Id],[SchoolId],[Name],[CreateTime] FROM [Student] WHERE ((LOWER([Name])) = (LOWER(@MethodConst0)) ) /***More Functions***/ //NBORM.IsNullOrEmpty(object thisValue) //NBORM.ToLower(object thisValue) //NBORM.string ToUpper(object thisValue) //NBORM.string Trim(object thisValue) //NBORM.bool Contains(string thisValue, string parameterValue) //NBORM.ContainsArray(object[] thisValue, string parameterValue) //NBORM.StartsWith(object thisValue, string parameterValue) //NBORM.EndsWith(object thisValue, string parameterValue) //NBORM.Equals(object thisValue, object parameterValue) //NBORM.DateIsSame(DateTime date1, DateTime date2) //NBORM.DateIsSame(DateTime date1, DateTime date2, DateType dataType) //NBORM.DateAdd(DateTime date, int addValue, DateType millisecond) //NBORM.DateAdd(DateTime date, int addValue) //NBORM.DateValue(DateTime date, DateType dataType) //NBORM.Between(object value, object start, object end) //NBORM.ToInt32(object value) //NBORM.ToInt64(object value) //NBORM.ToDate(object value) //NBORM.ToString(object value) //NBORM.ToDecimal(object value) //NBORM.ToGuid(object value) //NBORM.ToDouble(object value) //NBORM.ToBool(object value) //NBORM.Substring(object value, int index, int length) //NBORM.Replace(object value, string oldChar, string newChar) //NBORM.Length(object value) { throw new NotImplementedException(); } //NBORM.AggregateSum(object thisValue) //NBORM.AggregateAvg(TResult thisValue) //NBORM.AggregateMin(object thisValue) //NBORM.AggregateMax(object thisValue) //NBORM.AggregateCount(object thisValue) ```