コード例 #1
0
        public void Can_Chain_Order_Expressions_using_ThenBy()
        {
            db.Insert(People);

            var visitor = ReadExtensions.SqlExpression <Person>();

            visitor.OrderBy(x => x.Age);
            visitor.ThenBy(x => x.FirstName);

            var results = db.Select(visitor);

            Console.WriteLine("Sorting using Linq");
            var expected = People.OrderBy(x => x.Age).ThenBy(x => x.FirstName).ToList();

            foreach (var e in expected)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("Retrieved from DB");
            foreach (var r in results)
            {
                Console.WriteLine(r.ToString());
            }

            for (int i = 0; i < expected.Count(); i++)
            {
                if (results[i].Id != expected[i].Id)
                {
                    Assert.Fail("Expected person with id {0}, got {1}", expected[i].Id, results[i].Id);
                }
            }

            visitor.OrderBy(); //clears orderBy Expression

            visitor.OrderBy(x => x.Age);
            visitor.ThenByDescending(x => x.FirstName);
            results = db.Select(visitor);

            Console.WriteLine("Sorting using Linq");
            expected = People.OrderBy(x => x.Age).ThenByDescending(x => x.FirstName).ToList();
            foreach (var e in expected)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("Retrieved from DB");
            foreach (var r in results)
            {
                Console.WriteLine(r.ToString());
            }

            for (int i = 0; i < expected.Count(); i++)
            {
                if (results[i].Id != expected[i].Id)
                {
                    Assert.Fail("Expected person with id {0}, got {1}", expected[i].Id, results[i].Id);
                }
            }
        }
コード例 #2
0
        public void Can_Chain_Expressions_Using_And()
        {
            db.Insert(People);

            var visitor = ReadExtensions.SqlExpression <Person>();

            visitor.Where(x => x.FirstName.StartsWith("Jim")).And(x => x.LastName.StartsWith("Hen"));
            var results = db.Select <Person>(visitor);

            Assert.AreEqual(1, results.Count);

            visitor.Where(); //clears underlying expression

            visitor.Where(x => x.LastName.StartsWith("J")).And(x => x.Age > 40);
            results = db.Select(visitor);
            Assert.AreEqual(results[0].FirstName, "Michael");
        }
コード例 #3
0
        public void When_chaining_expressions_using_Where_it_behaves_like_And()
        {
            db.Insert(People);

            var visitor = ReadExtensions.SqlExpression <Person>();

            visitor.Where(x => x.FirstName.StartsWith("Jim"));
            visitor.Where(x => x.LastName.StartsWith("Hen"));
            //WHERE (upper("FirstName") like 'JIM%'  AND upper("LastName") like 'HEN%' )
            var results = db.Select <Person>(visitor);

            Assert.AreEqual(1, results.Count);

            visitor.Or(x => x.FirstName.StartsWith("M"));
            //WHERE ((upper("FirstName") like 'JIM%'  AND upper("LastName") like 'HEN%' ) OR upper("FirstName") like 'M%' )
            results = db.Select(visitor);
            Assert.AreEqual(2, results.Count);

            visitor.Where(x => x.FirstName.StartsWith("M"));
            //WHERE (((upper("FirstName") like 'JIM%'  AND upper("LastName") like 'HEN%' ) OR upper("FirstName") like 'M%' ) AND upper("FirstName") like 'M%' )
            results = db.Select(visitor);
            Assert.AreEqual(1, results.Count);
        }