コード例 #1
0
        public void GroupBy()
        {
            //Arrange
            var testList = User.GenerateSampleModels(100);
            var qry      = testList.AsQueryable();

            //Act
            var byAgeReturnUserName = qry.GroupBy("Profile.Age", "UserName");
            var byAgeReturnAll      = qry.GroupBy("Profile.Age");

            //Assert
            Assert.AreEqual(testList.GroupBy(x => x.Profile.Age).Count(), byAgeReturnUserName.Count());
            Assert.AreEqual(testList.GroupBy(x => x.Profile.Age).Count(), byAgeReturnAll.Count());
        }
コード例 #2
0
        public void GroupByMany_LambdaExpressions()
        {
            var lst = new List <Tuple <int, int, int> >()
            {
                new Tuple <int, int, int>(1, 1, 1),
                new Tuple <int, int, int>(1, 1, 2),
                new Tuple <int, int, int>(1, 1, 3),
                new Tuple <int, int, int>(2, 2, 4),
                new Tuple <int, int, int>(2, 2, 5),
                new Tuple <int, int, int>(2, 2, 6),
                new Tuple <int, int, int>(2, 3, 7)
            };

            var sel = lst.AsQueryable().GroupByMany(x => x.Item1, x => x.Item2);

            Assert.AreEqual(sel.Count(), 2);
            Assert.AreEqual(sel.First().Subgroups.Count(), 1);
            Assert.AreEqual(sel.Skip(1).First().Subgroups.Count(), 2);
        }
コード例 #3
0
        public void Where()
        {
            //Arrange
            var testList = User.GenerateSampleModels(100, allowNullableProfiles: true);
            var qry      = testList.AsQueryable();


            //Act
            var userById         = qry.Where("Id=@0", testList[10].Id);
            var userByUserName   = qry.Where("UserName=\"User5\"");
            var nullProfileCount = qry.Where("Profile=null");
            var userByFirstName  = qry.Where("Profile!=null && Profile.FirstName=@0", testList[1].Profile.FirstName);


            //Assert
            Assert.AreEqual(testList[10], userById.Single());
            Assert.AreEqual(testList[5], userByUserName.Single());
            Assert.AreEqual(testList.Count(x => x.Profile == null), nullProfileCount.Count());
            Assert.AreEqual(testList[1], userByFirstName.Single());
        }
コード例 #4
0
        public void Join()
        {
            //Arrange
            Person magnus = new Person {
                Name = "Hedlund, Magnus"
            };
            Person terry = new Person {
                Name = "Adams, Terry"
            };
            Person charlotte = new Person {
                Name = "Weiss, Charlotte"
            };

            Pet barley = new Pet {
                Name = "Barley", Owner = terry
            };
            Pet boots = new Pet {
                Name = "Boots", Owner = terry
            };
            Pet whiskers = new Pet {
                Name = "Whiskers", Owner = charlotte
            };
            Pet daisy = new Pet {
                Name = "Daisy", Owner = magnus
            };

            List <Person> people = new List <Person> {
                magnus, terry, charlotte
            };
            List <Pet> pets = new List <Pet> {
                barley, boots, whiskers, daisy
            };


            //Act
            var realQuery = people.AsQueryable().Join(
                pets,
                person => person,
                pet => pet.Owner,
                (person, pet) =>
                new { OwnerName = person.Name, Pet = pet.Name });

            var dynamicQuery = people.AsQueryable().Join(
                pets,
                "it",
                "Owner",
                "new(outer.Name as OwnerName, inner.Name as Pet)");

            //Assert
            var realResult = realQuery.ToArray();

#if NET35 || DNXCORE50
            var dynamicResult = dynamicQuery.Cast <object>().ToArray();

            Assert.AreEqual(realResult.Length, dynamicResult.Length);
            for (int i = 0; i < realResult.Length; i++)
            {
                Assert.AreEqual(realResult[i].OwnerName, dynamicResult[i].GetDynamicProperty <string>("OwnerName"));
                Assert.AreEqual(realResult[i].Pet, dynamicResult[i].GetDynamicProperty <string>("Pet"));
            }
#else
            var dynamicResult = dynamicQuery.ToDynamicArray();

            Assert.AreEqual(realResult.Length, dynamicResult.Length);
            for (int i = 0; i < realResult.Length; i++)
            {
                Assert.AreEqual(realResult[i].OwnerName, dynamicResult[i].OwnerName);
                Assert.AreEqual(realResult[i].Pet, dynamicResult[i].Pet);
            }
#endif
        }