SqlSugar 用法测试

2020/10/23 09:22
阅读数 96

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

接着上篇博客案例:https://www.cnblogs.com/chenwolong/p/SqlSugar.html

简单查询:

static SugarDbContext sugar = new SugarDbContext();
        static void Main(string[] args)
        {
            var db = sugar.Db;
            //查询所有
            var AllStudent = db.Queryable<Student>().ToList();
            //取前5条
            var top5 = db.Queryable<Student>().Take(5).ToList();
            //无锁查询
            var getAllNoLock = db.Queryable<Student>().With(SqlWith.NoLock).ToList();
            //根据主键查询
            var getByPrimaryKey = db.Queryable<Student>().InSingle("0000d82f-b1f2-4c7d-a3b9-6c70f9678282");
            //查询单条没有数据返回NULL, Single超过1条会报错,First不会
            //var getSingleOrDefault = db.Queryable<Student>().Single(); //会报错,数据量大于一条
            var getSingleOrDefault = db.Queryable<Student>().Where(A=>A.StudentID== "0000d82f-b1f2-4c7d-a3b9-6c70f9678282").Single();
            var getFirstOrDefault = db.Queryable<Student>().First();
            //UNION ALL Count = 2420838  240多万条数据
            var UNIONLst = db.UnionAll<Student>(db.Queryable<Student>(), db.Queryable<Student>()).ToList();
            //in 查询
            var in1 = db.Queryable<Student>().In(A => A.StudentID, new string[] { "000136bf-f968-4a59-9091-bae8ebca42fb", "00020ba7-44e6-494c-8fcb-c1be288a39b3" }).ToList();
            //主键 In (1,2,3)  不指定列, 默认根据主键In
            var in2 = db.Queryable<Student>().In(new string[] { "000136bf-f968-4a59-9091-bae8ebca42fb", "00020ba7-44e6-494c-8fcb-c1be288a39b3" }).ToList();
            //in 查询
            List<string> array = new List<string>{ "000136bf-f968-4a59-9091-bae8ebca42fb", "00020ba7-44e6-494c-8fcb-c1be288a39b3" };
            var in3 = db.Queryable<Student>().Where(it => array.Contains(it.StudentID)).ToList();
            //not in
            var in4 = db.Queryable<Student>().Where(it => !array.Contains(it.StudentID)).ToList();
            //where
            var getByWhere = db.Queryable<Student>().Where(it => it.StudentID == "000136bf-f968-4a59-9091-bae8ebca42fb" || it.StudentName == "陈丽").ToList();
            //SqlFunc
            var getByFuns = db.Queryable<Student>().Where(it => SqlFunc.IsNullOrEmpty(it.StudentName)).ToList();
            //between and 
            var between = db.Queryable<Student>().Where(it => SqlFunc.Between(it.CreateTime, DateTime.Now.AddDays(-10), DateTime.Now)).ToList();
            //排序
            var getAllOrder = db.Queryable<Student>().Take(100).OrderBy(it => it.CreateTime).ToList(); //默认为ASC排序
            //组合排序
            var data = db.Queryable<Student>()
                .OrderBy(it => it.StudentName, OrderByType.Asc)
                .OrderBy(it => it.CreateTime, OrderByType.Desc)
                .ToList();

            //是否存在 any
            var isAny = db.Queryable<Student>().Where(it => it.StudentName == "张龙").Any();
            var isAny2 = db.Queryable<Student>().Any(it => it.StudentSex == "");
            //获取同一天的记录
             var getTodayList = db.Queryable<Student>().Where(it => SqlFunc.DateIsSame(it.CreateTime, DateTime.Now)).ToList();

            Console.ReadLine();
        }
View Code

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部