Exemplo n.º 1
0
        private static void EasyExamples()
        {
            Console.WriteLine("");
            Console.WriteLine("#### Examples Start ####");
            var db                 = GetInstance();
            var dbTime             = db.GetDate();
            var getAll             = db.Queryable <Order>().Where(it => SqlFunc.EqualsNull(it.Name, null)).ToList();
            var getOrderBy         = db.Queryable <Order>().OrderBy(it => it.Name, OrderByType.Desc).ToList();
            var getOrderBy2        = db.Queryable <Order>().OrderBy(it => it.Id).OrderBy(it => it.Name, OrderByType.Desc).ToList();
            var getOrderBy3        = db.Queryable <Order>().OrderBy(it => new { it.Name, it.Id }).ToList();
            var getRandom          = db.Queryable <Order>().OrderBy(it => SqlFunc.GetRandom()).First();
            var getByPrimaryKey    = db.Queryable <Order>().InSingle(2);
            var getSingleOrDefault = db.Queryable <Order>().Where(it => it.Id == 1).Single();
            var getFirstOrDefault  = db.Queryable <Order>().First();
            var getByWhere         = db.Queryable <Order>().Where(it => it.Id == 1 || it.Name == "a").ToList();
            var getByWhere2        = db.Queryable <Order>().Where(it => it.Id == DateTime.Now.Year).ToList();
            var getByFuns          = db.Queryable <Order>().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();
            var getByFuns2         = db.Queryable <Order>().GroupBy(it => it.Name).Select(it => SqlFunc.AggregateDistinctCount(it.Price)).ToList();
            var getDicionary       = db.Queryable <Order>().ToDictionary(it => it.Id, it => it.Name);
            var getDicionaryList   = db.Queryable <Order>().ToDictionaryList();
            var getTest            = db.Queryable <Order>().Where(it => string.IsNullOrWhiteSpace(it.Name)).ToList();
            var test01             = db.Queryable <Order>().PartitionBy(it => it.Id).ToList();
            var q1                 = db.Queryable <Order>().Take(1);
            var q2                 = db.Queryable <Order>().Take(2);
            var test02             = db.Union(q1, q2).ToList();
            var test03             = db.Queryable <Order>().Take(1).ToList();
            var dp                 = DateTime.Now;
            var test05             = db.Queryable <Order>().Where(it => it.CreateTime.Month == dp.Month).ToList();
            var test06             = db.Queryable <Order>()
                                     .ToPivotTable(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));

            var test07 = db.Queryable <Order>()
                         .ToPivotList(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));

            var test08 = db.Queryable <Order>()
                         .ToPivotJson(it => it.Id, it => it.Name, it => it.Sum(x => x.Price));

            var test09 = db.Queryable <Order>().PartitionBy(it => it.Id).ToPageListAsync(1, 2, 0);

            test09.Wait();

            int c      = 0;
            var test10 = db.Queryable <Order>().ToPageList(1, 2, ref c);
            var test11 = db.Queryable <Order>().GroupBy(it => new { it.CreateTime.Year }).Select(it => it.CreateTime.Year).ToList();
            var test12 = db.Queryable <Order>().GroupBy(it => it.CreateTime.Date).Select(it => it.CreateTime.Date).ToList();
            var test13 = db.Queryable <Order>().GroupBy(it => new { it.CreateTime.Date, it.CreateTime.Year, it.CreateTime.Minute })
                         .Select(it => new { it.CreateTime.Date, it.CreateTime.Year, it.CreateTime.Minute }).ToList();
            var test14 = db.Queryable <Order>()
                         .GroupBy(it => it.CreateTime.Year)
                         .GroupBy(it => it.CreateTime.Second)
                         .GroupBy(it => it.CreateTime.Date)
                         .Select(it => new {
                it.CreateTime.Year,
                it.CreateTime.Second,
                it.CreateTime.Date
            }).ToList();
            var test15 = db.Queryable <Order, Order>((o, i) => new JoinQueryInfos(
                                                         JoinType.Left, o.Name == SqlFunc.ToString(SqlFunc.MergeString(",", i.Name, ","))
                                                         ))
                         .Select <ViewOrder>().ToList();
            var test16 = db.Queryable <Order>().Select(it => SqlFunc.SqlServer_DateDiff("day", DateTime.Now.AddDays(-1), DateTime.Now)).ToList();
            var test17 =
                db.Queryable <Order>()
                .Select <Order>()
                .MergeTable()
                .Select(it => new ViewOrder()
            {
                Name = SqlFunc.Subqueryable <Order>().Select(s => s.Name)
            }).ToList();;
            var test18 = db.UnionAll(
                db.Queryable <Order>(),
                db.Queryable <Order>()
                )
                         .Select(it => new ViewOrder()
            {
                Name = SqlFunc.Subqueryable <Order>().Select(s => s.Name)
            }).ToList();
            var test19 = db.Queryable <Order>().Select <ViewOrder>().ToList();
            var test20 = db.Queryable <Order>().LeftJoin <Custom>((o, cs) => o.Id == cs.Id)
                         .ToDictionary(it => it.Id, it => it.Name);

            var          test21 = db.Queryable <Order>().Where(it => it.Id.ToString() == 1.ToString()).Select(it => it.CreateTime.ToString("24")).First();
            var          test22 = db.Queryable <Order>().Where(it => it.Id.ToString() == 1.ToString()).Select(it => SqlFunc.AggregateDistinctCount(it.CreateTime)).First();
            var          test23 = db.Queryable <Order>().Where(it => true).Select(it => new { x1 = it.CreateTime.ToString("yyyy-MM"), it.CreateTime }).ToList();
            var          test24 = db.Queryable <Order>().Where(it => true).Select(it => new { x1 = it.CreateTime.ToString("yyyy-MM-dd _ HH _ mm _ ss "), it.CreateTime }).ToList();
            var          test25 = db.Queryable <Order>().Where(it => true).Select(it => new { x1 = it.CreateTime.Month, x2 = DateTime.Now.Month }).ToList();
            var          test26 = db.Queryable <Order>().Where(it => true).Select(it => new { x1 = it.CreateTime.Day, x2 = DateTime.Now.Day }).ToList();
            var          test27 = db.Queryable <Order>().Where(it => true).Select(it => new { x1 = it.CreateTime.Year, x2 = DateTime.Now.Year }).ToList();
            var          test28 = db.Queryable <Order>().Select(it => SqlFunc.DateDiff(DateType.Day, Convert.ToDateTime("2021-1-1"), Convert.ToDateTime("2021-1-12"))).ToList();
            var          test29 = db.Queryable <Order>().Select(it => new { x = SqlFunc.LessThan(1, 2) }).ToList();
            var          test30 = db.Queryable <Order>().Select(it => new { x = SqlFunc.LessThanOrEqual(1, 2) }).ToList();
            var          test31 = db.Queryable <Order>().Select(it => new { x = SqlFunc.GreaterThan(1, 2) }).ToList();
            var          test32 = db.Queryable <Order>().Select(it => new { x = SqlFunc.GreaterThanOrEqual(1, 2) }).ToList();
            List <Order> result = new List <Order>();

            db.Queryable <Order>().ForEach(it =>
            {
                result.Add(it);
            }, 10);
            result = new List <Order>();
            int count = 0;

            db.Queryable <Order>().ForEachByPage(it =>
            {
                result.Add(it);
            }, 2, 10, ref count, 5);
            var test33 = db.Queryable <Order>().ToList();

            db.CurrentConnectionConfig.SqlMiddle = new SqlMiddle
            {
                IsSqlMiddle    = true,
                ExecuteCommand = (s, p) => { return(s.Length); }
            };
            var five = db.Ado.ExecuteCommand("11111");

            db.CurrentConnectionConfig.SqlMiddle = null;
            Console.WriteLine("#### Examples End ####");
        }