コード例 #1
0
        public void OrderByTest()
        {
            using (IBulkedDbConnection bulk = FConnection.CreateBulkedDbConnection())
            {
                bulk.Insert(new OrmType {
                    Id = Guid.NewGuid(), Order = 1
                }, new OrmType {
                    Id = Guid.NewGuid(), Order = 2
                });

                bulk.Flush();
            }

            SqlExpression <OrmType> expression = FConnection.From <OrmType>();

            expression.PrefixFieldWithTableName = true;

            ISqlQuery query = new OrmLiteSqlQuery(expression);

            query.Select(typeof(OrmType).GetProperty(nameof(OrmType.Order)), typeof(OrmType).GetProperty(nameof(OrmType.Order)));
            query.OrderBy(typeof(OrmType).GetProperty(nameof(OrmType.Order)));
            query.OrderByDescending(typeof(OrmType).GetProperty(nameof(OrmType.Id)));

            List <OrmType> result = query.Run <OrmType>(FConnection);

            Assert.That(result.Count, Is.EqualTo(2));
            Assert.That(result[0].Order, Is.EqualTo(1));
            Assert.That(result[1].Order, Is.EqualTo(2));
        }
コード例 #2
0
        public void JoinTest()
        {
            Guid id = Guid.NewGuid();

            using (IBulkedDbConnection bulk = FConnection.CreateBulkedDbConnection())
            {
                bulk.Insert(new OrmType {
                    Id = id
                });
                bulk.Insert(new OrmTypeWithReference {
                    Id = Guid.NewGuid(), Reference = id
                });

                bulk.Flush();
            }

            SqlExpression <OrmType> expression = FConnection.From <OrmType>();

            ISqlQuery query = new OrmLiteSqlQuery(expression);

            query.Select(typeof(OrmType).GetProperty(nameof(OrmType.Id)), typeof(MyView).GetProperty(nameof(MyView.Azonosito)));
            query.InnerJoin(typeof(OrmType).GetProperty(nameof(OrmType.Id)), typeof(OrmTypeWithReference).GetProperty(nameof(OrmTypeWithReference.Reference)));

            List <MyView> result = query.Run <MyView>(FConnection);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result.Single().Azonosito, Is.EqualTo(id));
        }
コード例 #3
0
        public void AggregateTest()
        {
            Guid reference = Guid.NewGuid();

            using (IBulkedDbConnection bulk = FConnection.CreateBulkedDbConnection())
            {
                bulk.Insert(new OrmType {
                    Id = reference
                });

                bulk.InsertAll(new[]
                {
                    new OrmTypeWithReference {
                        Id = Guid.NewGuid(), Reference = reference
                    },
                    new OrmTypeWithReference {
                        Id = Guid.NewGuid(), Reference = reference
                    }
                });

                bulk.Flush();
            }

            SqlExpression <OrmTypeWithReference> expression = FConnection.From <OrmTypeWithReference>();

            ISqlQuery query = new OrmLiteSqlQuery(expression);

            query.GroupBy(typeof(OrmTypeWithReference).GetProperty(nameof(OrmTypeWithReference.Reference)));
            query.SelectCount(typeof(OrmTypeWithReference).GetProperty(nameof(OrmTypeWithReference.Id)), typeof(CountView).GetProperty(nameof(CountView.Count)));

            List <CountView> result = query.Run <CountView>(FConnection);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result.Single().Count, Is.EqualTo(2));
        }
コード例 #4
0
        /// <summary>
        /// Queries the given <typeparamref name="TView"/>.
        /// </summary>
        public static List <TView> Query <TView>(this IDbConnection connection, Action <IUntypedSqlExpression>?additions = null)
        {
            OrmLiteSqlQuery query = SmartSqlBuilder <TView> .Build(from => new OrmLiteSqlQuery(from));

            additions?.Invoke(query.UnderlyingExpression);
            return(query.Run <TView>(connection));
        }
コード例 #5
0
        public void SelectTest()
        {
            Guid id = Guid.NewGuid();

            FConnection.Insert(new OrmType {
                Id = id
            });

            SqlExpression <OrmType> expression = FConnection.From <OrmType>();

            ISqlQuery query = new OrmLiteSqlQuery(expression);

            query.Select(typeof(OrmType).GetProperty(nameof(OrmType.Id)), typeof(MyView).GetProperty(nameof(MyView.Azonosito)));

            List <MyView> result = query.Run <MyView>(FConnection);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result.Single().Azonosito, Is.EqualTo(id));
        }