Beispiel #1
0
        /// <summary>
        /// Sql查询
        /// </summary>
        private void SqlQuery()
        {
            using (var db = SugarDao.GetInstance())
            {
                //转成list
                List <Student> list1 = db.SqlQuery <Student>("select * from Student");
                //转成list带参
                List <Student> list2 = db.SqlQuery <Student>("select * from Student where id=@id", new { id = 1 });
                //转成dynamic
                dynamic list3 = db.SqlQueryDynamic("select * from student");
                //转成json
                string list4 = db.SqlQueryJson("select * from student");
                //返回int
                var list5 = db.SqlQuery <int>("select top 1 id from Student").SingleOrDefault();
                //反回键值
                Dictionary <string, string> list6 = db.SqlQuery <KeyValuePair <string, string> >("select id,name from Student").ToDictionary(it => it.Key, it => it.Value);
                //反回List<string[]>
                var list7 = db.SqlQuery <string[]>("select top 1 id,name from Student").SingleOrDefault();
                //存储过程
                var spResult = db.SqlQuery <School>("exec sp_school @p1,@p2", new { p1 = 1, p2 = 2 });

                //获取第一行第一列的值
                string  v1 = db.GetString("select '张三' as name");
                int     v2 = db.GetInt("select 1 as name");
                double  v3 = db.GetDouble("select 1 as name");
                decimal v4 = db.GetDecimal("select 1 as name");
            }
        }
Beispiel #2
0
        public void Init()
        {
            using (SqlSugarClient db = SugarDao.GetInstance())//开启数据库连接
            {
                Student s = new Student()
                {
                    name = "张" + new Random().Next(1, int.MaxValue)
                };

                db.Insert(s); //插入一条记录 (有主键也好,没主键也好,有自增列也好都可以插进去)


                List <Student> list = new List <Student>()
                {
                    new Student()
                    {
                        name = "张" + new Random().Next(1, int.MaxValue)
                    },
                    new Student()
                    {
                        name = "张" + new Random().Next(1, int.MaxValue)
                    }
                };

                db.InsertRange(list); //批量插入
            }
        }
        /// <summary>
        /// 新容器转换
        /// </summary>
        private void NewSelect()
        {
            using (SqlSugarClient db = SugarDao.GetInstance())
            {
                var list2 = db.Queryable <Student>().Where(c => c.id < 10).Select(c => new classNew {
                    newid = c.id, newname = c.name, xx_name = c.name
                }).ToList();                                                                                                                                  //不支持匿名类转换,也不建议使用

                var list3 = db.Queryable <Student>().Where(c => c.id < 10).Select(c => new { newid = c.id, newname = c.name, xx_name = c.name }).ToDynamic(); //匿名类转换
            }
        }
Beispiel #4
0
 public void Init()
 {
     using (SqlSugarClient db = SugarDao.GetInstance())//开启数据库连接
     {
         var r1 = db.GetDataTable("select * from student");
         var r2 = db.GetSingle <Student>("select top 1 * from student");
         var r3 = db.GetScalar("select  count(1) from student");
         var r4 = db.GetReader("select  count(1) from student");
         r4.Dispose();
         var r5 = db.GetString("select  top 1 name from student");
         var r6 = db.ExecuteCommand("select 1");
     }
 }
        public void Init()
        {
            using (SqlSugarClient db = SugarDao.GetInstance())//开启数据库连接
            {
                Student s = new Student()
                {
                    name = "张" + new Random().Next(1, int.MaxValue)
                };

                db.Insert(s); //插入一条记录 (有主键也好,没主键也好,有自增列也好都可以插进去)


                List <Student> list = new List <Student>()
                {
                    new Student()
                    {
                        name = "张" + new Random().Next(1, int.MaxValue)
                    },
                    new Student()
                    {
                        name = "张" + new Random().Next(1, int.MaxValue)
                    }
                };

                db.InsertRange(list); //批量插入


                //高性能批量插入
                //注意:性能比InsertRange高,功能没有InsertRange全,不支持别名表和排除列
                using (SqlSugarClient db2 = SugarDao.GetInstance())//开启数据库连接
                {
                    var list2 = new List <Student>()
                    {
                        new Student()
                        {
                            isOk = true, name = "张三", sch_id = 1, sex = "男"
                        },
                        new Student()
                        {
                            isOk = true, name = "sun", sch_id = 1, sex = "女"
                        },
                        new Student()
                        {
                            isOk = true, name = "mama", sch_id = 1, sex = "gril"
                        }
                    };
                    db.SqlBulkCopy <Student>(list2);
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// 新容器转换
        /// </summary>
        private void NewSelect()
        {
            using (SqlSugarClient db = SugarDao.GetInstance())
            {
                var list2 = db.Queryable <Student>().Where(c => c.id < 10).Select(c => new classNew {
                    newid = c.id, newname = c.name, xx_name = c.name
                }).ToList();                                                                                                                                  //不支持匿名类转换,也不建议使用

                var list3 = db.Queryable <Student>().Where(c => c.id < 10).Select(c => new { newid = c.id, newname = c.name, xx_name = c.name }).ToDynamic(); //匿名类转换

                var list4 = db.Queryable <Student>().Where(c => c.id < 10).Select("id as newid, name as newname ,name as xx_name").ToDynamic();               //匿名类转换

                var jList1 = db.Queryable <Student>()
                             .JoinTable <Student, School>((s1, s2) => s1.sch_id == s2.id) // left join  School s2  on s1.id=s2.id
                             .Where <Student, School>((s1, s2) => s1.id > 1)              // where s1.id>1
                             .OrderBy <Student, School>((s1, s2) => s1.id)                //order by s1.id 多个order可以  .oderBy().orderby 叠加
                             .Skip(1)
                             .Take(2)
                             .Select <Student, School, classNew>((s1, s2) => new classNew()
                {
                    newid = s1.id, newname = s2.name, xx_name = s1.name
                })                                                                                                                     //select目前只支持这种写法
                             .ToList();

                var jList2 = db.Queryable <Student>()
                             .JoinTable <Student, School>((s1, s2) => s1.sch_id == s2.id) // left join  School s2  on s1.id=s2.id
                             .Where <Student, School>((s1, s2) => s1.id > 1)              // where s1.id>1
                             .OrderBy <Student, School>((s1, s2) => s1.id)                //order by s1.id 多个order可以  .oderBy().orderby 叠加
                             .Skip(1)
                             .Take(2)
                             .Select <Student, School, classNew>((s1, s2) => new classNew()
                {
                    newid = s1.id, newname = s1.name, xx_name = s1.name
                })                                                                                                                    //select目前只支持这种写法
                             .ToDynamic();


                var jList3 = db.Queryable <Student>()
                             .JoinTable <Student, School>((s1, s2) => s1.sch_id == s2.id) // left join  School s2  on s1.id=s2.id
                             .Where <Student, School>((s1, s2) => s1.id > 1)              // where s1.id>1
                             .OrderBy <Student, School>((s1, s2) => s1.id)                //order by s1.id 多个order可以  .oderBy().orderby 叠加
                             .Skip(1)
                             .Take(2)
                             .Select <Student, classNew>(s1 => new classNew()
                {
                    newid = s1.id, newname = s1.name, xx_name = s1.name
                })                                                                                                      //select目前只支持这种写法
                             .ToDynamic();
            }
        }
Beispiel #7
0
        public void Init()
        {
            using (var db = SugarDao.GetInstance())
            {
                //真删除
                db.Delete <School, int>(10);//注意主键必需为实体类的第一个属性
                db.Delete <School>(it => it.id > 100);
                db.Delete <School, string>(new string[] { "100", "101", "102" });

                //假删除
                //db.FalseDelete<school>("is_del", 100);
                //等同于 update school set is_del=0 where id in(100)
                //db.FalseDelete<school>("is_del", it=>it.id==100);
            }
        }
Beispiel #8
0
 public void Init()
 {
     using (SqlSugarClient db = SugarDao.GetInstance())
     {
         var stuList = db.Queryable <Student>().ToList();
         db.Insert <Student>(new Student()
         {
             sch_id = SchoolEnum.蓝翔2
         });
         db.Update <Student>(new Student()
         {
             sch_id = SchoolEnum.蓝翔1, id = 1
         });
         var stuList2 = db.Queryable <Student>().Where(it => it.sch_id == SchoolEnum.蓝翔1).ToList();
     }
 }
Beispiel #9
0
        public void Init()
        {
            using (var db = SugarDao.GetInstance())
            {
                //根据当前数据库生成所有表的实体类文件 (参数:SqlSugarClient ,文件目录,命名空间)
                db.ClassGenerating.CreateClassFiles(db, @"D:\SqlSugarModelFiles2", "Models");
                //根据表名生成实体类文件
                //db.ClassGenerating.CreateClassFilesByTableNames(db, Server.MapPath("~/Models"), "Models" , "student","school");

                //根据表名生成class字符串
                var str = db.ClassGenerating.TableNameToClass(db, "Student");

                //根据SQL语句生成class字符串
                var str2 = db.ClassGenerating.SqlToClass(db, "select top 1 * from Student", "student");
            }
        }
        public void Init()
        {
            using (SqlSugarClient db = SugarDao.GetInstance()) //开启数据库连接
            {
                db.IsNoLock       = true;                      //启用无锁查询
                db.CommandTimeOut = 30000;                     //设置超时时间
                try
                {
                    db.BeginTran();//开启事务
                    //db.BeginTran(IsolationLevel.ReadCommitted);+3重载可以设置事世隔离级别

                    db.CommitTran();//提交事务
                }
                catch (Exception)
                {
                    db.RollbackTran();//回滚事务
                    throw;
                }
            }
        }
        public void Init()
        {
            int id = 10;

            using (var db = SugarDao.GetInstance())
            {
                //指定列更新
                db.Update <School>(new { name = "蓝翔2" }, it => it.id == id);
                db.Update <School, int>(new { name = "蓝翔2" }, 10, 13, 12);
                db.Update <School, string>(new { name = "蓝翔2" }, new string[] { "11", "12" });

                //整个实体更新
                db.Update(new School {
                    id = id, name = "蓝翔2"
                });
                db.Update <School>(new School {
                    id = id, name = "蓝翔2"
                }, it => it.id == id);


                //设置不更新列
                db.DisableUpdateColumns = new string[] { "CreateTime" };//设置CreateTime不更新

                TestUpdateColumns updObj = new TestUpdateColumns()
                {
                    VGUID         = Guid.Parse("542b5a27-6984-47c7-a8ee-359e483c8470"),
                    Name          = "xx",
                    Name2         = "xx2",
                    IdentityField = 0,
                    CreateTime    = null
                };

                //CreateTime将不会被更新
                db.Update(updObj);
                //以前实现这种更新需要用指定列的方式实现,现在就简单多了。
            }
        }
Beispiel #12
0
        /// <summary>
        /// 单表查询
        /// </summary>
        private void QueryableDemo()
        {
            using (var db = SugarDao.GetInstance())
            {
                //---------Queryable<T>,扩展函数查询---------//

                //针对单表或者视图查询

                //查询所有
                var student        = db.Queryable <Student>().ToList();
                var studentDynamic = db.Queryable <Student>().ToDynamic();
                var studentJson    = db.Queryable <Student>().ToJson();

                //查询单条
                var single = db.Queryable <Student>().Single(c => c.id == 1);
                //查询单条没有记录返回空对象
                var single2 = db.Queryable <Student>().Where(c => c.id == 1).SingleOrDefault();

                //查询第一条
                var first  = db.Queryable <Student>().Where(c => c.id == 1).First();
                var first2 = db.Queryable <Student>().Where(c => c.id == 1).FirstOrDefault();

                //取10-20条
                var page1 = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).Skip(10).Take(20).ToList();

                //上一句的简化写法,同样取10-20条
                var page2 = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).ToPageList(2, 10);

                //查询条数
                var count = db.Queryable <Student>().Where(c => c.id > 10).Count();

                //从第2条开始以后取所有
                var skip = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).Skip(2).ToList();

                //取前2条
                var take = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).Take(2).ToList();

                // Not like
                string conval  = "a";
                var    notLike = db.Queryable <Student>().Where(c => !c.name.Contains(conval.ToString())).ToList();
                //Like
                conval = "三";
                var like = db.Queryable <Student>().Where(c => c.name.Contains(conval)).ToList();

                // 可以在拉姆达使用 ToString和 Convert,比EF出色的地方
                var convert1 = db.Queryable <Student>().Where(c => c.name == "a".ToString()).ToList();
                var convert2 = db.Queryable <Student>().Where(c => c.id == Convert.ToInt32("1")).ToList();//
                var convert3 = db.Queryable <Student>().Where(c => DateTime.Now > Convert.ToDateTime("2015-1-1")).ToList();
                var convert4 = db.Queryable <Student>().Where(c => DateTime.Now > DateTime.Now).ToList();

                //支持字符串Where 让你解决,更复杂的查询
                var student12 = db.Queryable <Student>().Where(c => "a" == "a").Where("id>100").ToList();
                var student13 = db.Queryable <Student>().Where(c => "a" == "a").Where("id>100 and id in( select 1)").ToList();


                //存在记录反回true,则否返回false
                bool isAny100 = db.Queryable <Student>().Any(c => c.id == 100);
                bool isAny1   = db.Queryable <Student>().Any(c => c.id == 1);


                //获取最大Id
                object maxId  = db.Queryable <Student>().Max(it => it.id);
                int    maxId1 = db.Queryable <Student>().Max(it => it.id).ObjToInt(); //拉姆达
                int    maxId2 = db.Queryable <Student>().Max <Student, int>("id");    //字符串写法

                //获取最小
                int minId1 = db.Queryable <Student>().Where(c => c.id > 0).Min(it => it.id).ObjToInt(); //拉姆达
                int minId2 = db.Queryable <Student>().Where(c => c.id > 0).Min <Student, int>("id");    //字符串写法


                //order By
                var orderList = db.Queryable <Student>().OrderBy("id desc,name asc").ToList();                                    //字符串支持多个排序
                //可以多个order by表达示
                var order2List = db.Queryable <Student>().OrderBy(it => it.name).OrderBy(it => it.id, OrderByType.desc).ToList(); // order by name as ,order by id desc

                //In
                var intArray = new[] { "5", "2", "3" };
                var intList  = intArray.ToList();
                var list0    = db.Queryable <Student>().In(it => it.id, 1, 2, 3).ToList();
                var list1    = db.Queryable <Student>().In(it => it.id, intArray).ToList();
                var list2    = db.Queryable <Student>().In("id", intArray).ToList();
                var list3    = db.Queryable <Student>().In(it => it.id, intList).ToList();
                var list4    = db.Queryable <Student>().In("id", intList).ToList();

                //分组查询
                var             list7  = db.Queryable <Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select("sex,Count=count(*)").ToDynamic();
                var             list8  = db.Queryable <Student>().Where(c => c.id < 20).GroupBy(it => it.sex).GroupBy(it => it.id).Select("id,sex,Count=count(*)").ToDynamic();
                List <SexTotal> list9  = db.Queryable <Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select <Student, SexTotal>("Sex,Count=count(*)").ToList();
                List <SexTotal> list10 = db.Queryable <Student>().Where(c => c.id < 20).GroupBy("sex").Select <Student, SexTotal>("Sex,Count=count(*)").ToList();
                //SELECT Sex,Count=count(*)  FROM Student  WHERE 1=1  AND  (id < 20)    GROUP BY Sex --生成结果


                //2表关联查询
                var jList = db.Queryable <Student>()
                            .JoinTable <Student, School>((s1, s2) => s1.sch_id == s2.id) //默认left join
                            .Where <Student, School>((s1, s2) => s1.id == 1)
                            .Select("s1.*,s2.name as schName")
                            .ToDynamic();

                /*等于同于
                 * SELECT s1.*,s2.name as schName
                 * FROM [Student]  s1
                 * LEFT JOIN [School]  s2 ON  s1.sch_id  = s2.id
                 * WHERE  s1.id  = 1 */

                //2表关联查询并分页
                var jList2 = db.Queryable <Student>()
                             .JoinTable <Student, School>((s1, s2) => s1.sch_id == s2.id) //默认left join
                                                                                          //如果要用inner join这么写
                                                                                          //.JoinTable<Student, School>((s1, s2) => s1.sch_id == s2.id  ,JoinType.INNER)
                             .Where <Student, School>((s1, s2) => s1.id > 1)
                             .OrderBy <Student, School>((s1, s2) => s1.name)
                             .Skip(10)
                             .Take(20)
                             .Select("s1.*,s2.name as schName")
                             .ToDynamic();

                //3表查询并分页
                var jList3 = db.Queryable <Student>()
                             .JoinTable <Student, School>((s1, s2) => s1.sch_id == s2.id) // left join  School s2  on s1.id=s2.id
                             .JoinTable <Student, School>((s1, s3) => s1.sch_id == s3.id) // left join  School s3  on s1.id=s3.id
                             .Where <Student, School>((s1, s2) => s1.id > 1)              // where s1.id>1
                             .Where <Student>(s1 => s1.id > 0)
                             .OrderBy <Student, School>((s1, s2) => s1.id)                //order by s1.id 多个order可以  .oderBy().orderby 叠加
                             .Skip(10)
                             .Take(20)
                             .Select("s1.*,s2.name as schName,s3.name as schName2")//select目前只支持这种写法
                             .ToDynamic();


                //上面的方式都是与第一张表join,第三张表想与第二张表join写法如下
                List <classNew> jList4 = db.Queryable <Student>()
                                         .JoinTable <Student, School>((s1, s2) => s1.sch_id == s2.id)           // left join  School s2  on s1.id=s2.id
                                         .JoinTable <Student, School, Area>((s1, s2, a1) => a1.id == s2.AreaId) // left join  Area a1  on a1.id=s2.AreaId
                                         .Select <Student, School, Area, classNew>((s1, s2, a1) => new classNew {
                    newid = s1.id, studentName = s1.name, schoolName = s2.name, areaName = a1.name
                }).ToList();



                //最多支持5表查询,太过复杂的建议用Sqlable或者SqlQuery,我们的Queryable只适合轻量级的查询



                //拼接
                var queryable = db.Queryable <Student>().Where(it => true);
                if (maxId.ObjToInt() == 1)
                {
                    queryable.Where(it => it.id == 1);
                }
                else
                {
                    queryable.Where(it => it.id == 2);
                }
                var listJoin = queryable.ToList();


                //queryable和SqlSugarClient解耦
                var par = new Queryable <Student>().Where(it => it.id == 1);//声名没有connection对象的Queryable
                par.DB = db;
                var listPar = par.ToList();
            }
        }
Beispiel #13
0
        /// <summary>
        /// 多表查询
        /// </summary>
        private void SqlableDemo()
        {
            using (var db = SugarDao.GetInstance())
            {
                //---------Sqlable,创建多表查询---------//

                //多表查询
                List <School> dataList = db.Sqlable()
                                         .From("school", "s")
                                         .Join("student", "st", "st.id", "s.id", JoinType.INNER)
                                         .Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
                                         .Where("s.id>100 and s.id<@id")
                                         .Where("1=1")//可以多个WHERE
                                         .OrderBy("ID")
                                         .SelectToList <School /*新的Model我这里没有所以写的School*/>("st.*", new { id = 1 });

                //多表分页
                List <School> dataPageList = db.Sqlable()
                                             .From("school", "s")
                                             .Join("student", "st", "st.id", "s.id", JoinType.INNER)
                                             .Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
                                             .Where("s.id>100 and s.id<100")
                                             .SelectToPageList <School>("st.*", "s.id", 1, 10);

                //多表分页WHERE加子查询
                List <School> dataPageList2 = db.Sqlable()
                                              .From("school", "s")
                                              .Join("student", "st", "st.id", "s.id", JoinType.INNER)
                                              .Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
                                              .Where("s.id>100 and s.id<100 and s.id in (select 1 )" /*这里面写子查询都可以*/)
                                              .SelectToPageList <School>("st.*", "s.id", 1, 10);



                //--------转成List Dynmaic 或者 Json-----//

                //不分页
                var list1 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDynamic("*", new { id = 1 });
                var list2 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToJson("*", new { id = 1 });
                var list3 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDataTable("*", new { id = 1 });

                //分页
                var list4 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", 1, 10, new { id = 1 });
                var list5 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageTable("s.*", "l.id", 1, 10, new { id = 1 });
                var list6 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", 1, 10, new { id = 1 });


                //--------拼接-----//
                Sqlable sable = db.Sqlable().From <Student>("s").Join <School>("l", "s.sch_id", "l.id", JoinType.INNER);
                string  name  = "a";
                int     id    = 1;
                if (!string.IsNullOrEmpty(name))
                {
                    sable = sable.Where("s.name=@name");
                }
                if (!string.IsNullOrEmpty(name))
                {
                    sable = sable.Where("s.id=@id or s.id=100");
                }
                if (id > 0)
                {
                    sable = sable.Where("l.id in (select top 10 id from school)");//where加子查询
                }
                var pars      = new { id = id, name = name };
                int pageCount = sable.Count(pars);
                var list7     = sable.SelectToPageList <Student>("s.*", "l.id desc", 1, 20, pars);
            }
        }
        /// <summary>
        /// 单表查询
        /// </summary>
        private void QueryableDemo()
        {
            using (var db = SugarDao.GetInstance())
            {
                //---------Queryable<T>,扩展函数查询---------//

                //针对单表或者视图查询

                //查询所有
                var student        = db.Queryable <Student>().ToList();
                var studentDynamic = db.Queryable <Student>().ToDynamic();
                var studentJson    = db.Queryable <Student>().ToJson();

                //查询单条
                var single = db.Queryable <Student>().Single(c => c.id == 1);
                //查询单条没有记录返回空对象
                var single2 = db.Queryable <Student>().Where(c => c.id == 1).SingleOrDefault();

                //查询第一条
                var first  = db.Queryable <Student>().Where(c => c.id == 1).First();
                var first2 = db.Queryable <Student>().Where(c => c.id == 1).FirstOrDefault();

                //取10-20条
                var page1 = db.Queryable <Student>().Where(c => c.id > 10).OrderBy("id").Skip(10).Take(20).ToList();

                //上一句的简化写法,同样取10-20条
                var page2 = db.Queryable <Student>().Where(c => c.id > 10).OrderBy("id").ToPageList(2, 10);

                //查询条数
                var count = db.Queryable <Student>().Where(c => c.id > 10).Count();

                //从第2条开始以后取所有
                var skip = db.Queryable <Student>().Where(c => c.id > 10).OrderBy("id").Skip(2).ToList();

                //取前2条
                var take = db.Queryable <Student>().Where(c => c.id > 10).OrderBy("id").Take(2).ToList();

                // Not like
                string conval  = "a";
                var    notLike = db.Queryable <Student>().Where(c => !c.name.Contains(conval.ToString())).ToList();
                //Like
                conval = "三";
                var like = db.Queryable <Student>().Where(c => c.name.Contains(conval)).ToList();

                // 可以在拉姆达使用 ToString和 Convert,比EF出色的地方
                var convert1 = db.Queryable <Student>().Where(c => c.name == "a".ToString()).ToList();
                var convert2 = db.Queryable <Student>().Where(c => c.id == Convert.ToInt32("1")).ToList();//
                var convert3 = db.Queryable <Student>().Where(c => DateTime.Now > Convert.ToDateTime("2015-1-1")).ToList();
                var convert4 = db.Queryable <Student>().Where(c => DateTime.Now > DateTime.Now).ToList();

                //支持字符串Where 让你解决,更复杂的查询
                var student12 = db.Queryable <Student>().Where(c => "a" == "a").Where("id>100").ToList();
                var student13 = db.Queryable <Student>().Where(c => "a" == "a").Where("id>100 and id in( select 1)").ToList();


                //存在记录反回true,则否返回false
                bool isAny100 = db.Queryable <Student>().Any(c => c.id == 100);
                bool isAny1   = db.Queryable <Student>().Any(c => c.id == 1);

                int maxId = db.Queryable <Student>().Max <Student, int>("id");
                int minId = db.Queryable <Student>().Where(c => c.id > 0).Min <Student, int>("id");



                //In
                var list1 = db.Queryable <Student>().In("id", "1", "2", "3").ToList();
                var list2 = db.Queryable <Student>().In("id", new string[] { "1", "2", "3" }).ToList();
                var list3 = db.Queryable <Student>().In("id", new List <string> {
                    "1", "2", "3"
                }).ToList();
                var list4 = db.Queryable <Student>().Where(it => it.id < 10).In("id", new List <string> {
                    "1", "2", "3"
                }).ToList();

                //分组查询
                var list5 = db.Queryable <Student>().Where(c => c.id < 20).GroupBy("sex").Select <Student, SexTotal>("Sex,Count=count(*)").ToList();
                //SELECT Sex,Count=count(*)  FROM Student  WHERE 1=1  AND  (id < 20)    GROUP BY Sex --生成结果
            }
        }
Beispiel #15
0
        /// <summary>
        /// 单表查询
        /// </summary>
        private void QueryableDemo()
        {
            using (var db = SugarDao.GetInstance())
            {
                //---------Queryable<T>,扩展函数查询---------//

                //针对单表或者视图查询

                //查询所有
                var student        = db.Queryable <Student>().ToList();
                var studentDynamic = db.Queryable <Student>().ToDynamic();
                var studentJson    = db.Queryable <Student>().ToJson();

                //查询单条
                var single = db.Queryable <Student>().Single(c => c.id == 1);
                //查询单条没有记录返回空对象
                var single2 = db.Queryable <Student>().Where(c => c.id == 1).SingleOrDefault();

                //查询第一条
                var first  = db.Queryable <Student>().Where(c => c.id == 1).First();
                var first2 = db.Queryable <Student>().Where(c => c.id == 1).FirstOrDefault();

                //取10-20条
                var page1 = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).Skip(10).Take(20).ToList();

                //上一句的简化写法,同样取10-20条
                var page2 = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).ToPageList(2, 10);

                //查询条数
                var count = db.Queryable <Student>().Where(c => c.id > 10).Count();

                //从第2条开始以后取所有
                var skip = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).Skip(2).ToList();

                //取前2条
                var take = db.Queryable <Student>().Where(c => c.id > 10).OrderBy(it => it.id).Take(2).ToList();

                // Not like
                string conval  = "a";
                var    notLike = db.Queryable <Student>().Where(c => !c.name.Contains(conval.ToString())).ToList();
                //Like
                conval = "三";
                var like = db.Queryable <Student>().Where(c => c.name.Contains(conval)).ToList();

                // 可以在拉姆达使用 ToString和 Convert,比EF出色的地方
                var convert1 = db.Queryable <Student>().Where(c => c.name == "a".ToString()).ToList();
                var convert2 = db.Queryable <Student>().Where(c => c.id == Convert.ToInt32("1")).ToList();//
                var convert3 = db.Queryable <Student>().Where(c => DateTime.Now > Convert.ToDateTime("2015-1-1")).ToList();
                var convert4 = db.Queryable <Student>().Where(c => DateTime.Now > DateTime.Now).ToList();

                //支持字符串Where 让你解决,更复杂的查询
                var student12 = db.Queryable <Student>().Where(c => "a" == "a").Where("id>100").ToList();
                var student13 = db.Queryable <Student>().Where(c => "a" == "a").Where("id>100 and id in( select 1)").ToList();


                //存在记录反回true,则否返回false
                bool isAny100 = db.Queryable <Student>().Any(c => c.id == 100);
                bool isAny1   = db.Queryable <Student>().Any(c => c.id == 1);


                //获取最大Id
                object maxId  = db.Queryable <Student>().Max(it => it.id);
                int    maxId1 = db.Queryable <Student>().Max(it => it.id).ObjToInt(); //拉姆达
                int    maxId2 = db.Queryable <Student>().Max <Student, int>("id");    //字符串写法

                //获取最小
                int minId1 = db.Queryable <Student>().Where(c => c.id > 0).Min(it => it.id).ObjToInt(); //拉姆达
                int minId2 = db.Queryable <Student>().Where(c => c.id > 0).Min <Student, int>("id");    //字符串写法


                //order By
                var orderList = db.Queryable <Student>().OrderBy("id desc,name asc").ToList();                                    //字符串支持多个排序
                //可以多个order by表达示
                var order2List = db.Queryable <Student>().OrderBy(it => it.name).OrderBy(it => it.id, OrderByType.desc).ToList(); // order by name as ,order by id desc

                //In
                var intArray = new[] { "5", "2", "3" };
                var intList  = intArray.ToList();
                var list0    = db.Queryable <Student>().In(it => it.id, 1, 2, 3).ToList();
                var list1    = db.Queryable <Student>().In(it => it.id, intArray).ToList();
                var list2    = db.Queryable <Student>().In("id", intArray).ToList();
                var list3    = db.Queryable <Student>().In(it => it.id, intList).ToList();
                var list4    = db.Queryable <Student>().In("id", intList).ToList();

                //分组查询
                var             list7  = db.Queryable <Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select("sex,Count=count(*)").ToDynamic();
                var             list8  = db.Queryable <Student>().Where(c => c.id < 20).GroupBy(it => it.sex).GroupBy(it => it.id).Select("id,sex,Count=count(*)").ToDynamic();
                List <SexTotal> list9  = db.Queryable <Student>().Where(c => c.id < 20).GroupBy(it => it.sex).Select <Student, SexTotal>("Sex,Count=count(*)").ToList();
                List <SexTotal> list10 = db.Queryable <Student>().Where(c => c.id < 20).GroupBy("sex").Select <Student, SexTotal>("Sex,Count=count(*)").ToList();
                //SELECT Sex,Count=count(*)  FROM Student  WHERE 1=1  AND  (id < 20)    GROUP BY Sex --生成结果
            }
        }