예제 #1
0
        public void AnExampleOfThenBy()
        {
            var people = GetPeople().ToList();

            people.Add(new Person()
            {
                Name = "Bob", Age = 5
            });

            var expected = new[] { new Person()
                                   {
                                       Name = "Alice", Age = 29
                                   },
                                   new Person()
                                   {
                                       Name = "Bob", Age = 5
                                   },
                                   new Person()
                                   {
                                       Name = "Bob", Age = 40
                                   },
                                   new Person()
                                   {
                                       Name = "Chuck", Age = 25
                                   } };


            var result = people.OrderBy(x => x.Name).ThenBy(x => x.Age);

            BetterAssert.AreEqual(expected, result);
        }
예제 #2
0
        public void AnExampleOfOrderBy()
        {
            var people   = GetPeople();
            var expected = new [] { "Alice", "Bob", "Chuck" };

            var result = people.OrderBy(x => x.Name).Select(x => x.Name);

            BetterAssert.AreEqual(expected, result);
        }
예제 #3
0
        public void AnExampleOfTake()
        {
            var numbers  = Enumerable.Range(1, 100);
            var expected = Enumerable.Range(1, 10);

            var result = numbers.Take(10);

            BetterAssert.AreEqual(expected, result);
        }
예제 #4
0
        public void AnExampleOfSkip()
        {
            var numbers  = new[] { 2, 4, 6, 8, 10 };
            var expected = new[] { 8, 10 };

            var result = numbers.Skip(3);

            BetterAssert.AreEqual(expected, result);
        }
예제 #5
0
        public void AnExampleOfZip()
        {
            var letters  = new[] { "A", "B", "C", "D", "E" };
            var expected = new[] { "4 = A", "7 = B", "2 = C", "5 = D", "9 = E" };

            var result = Numbers.Zip(letters, (x, y) => x + " = " + y);

            BetterAssert.AreEqual(expected, result);
        }
        public void TheSameExampleOfSelectManyInMethodSyntax()
        {
            var list     = new[] { "1,2,3", "4,5", "6,7,8" };
            var expected = new[] { "1", "2", "3", "4", "5", "6", "7", "8" };

            var result = list.SelectMany(item => item.Split(','));

            BetterAssert.AreEqual(expected, result);
        }
예제 #7
0
        public void AnExampleOfOrderByDescending()
        {
            var people   = GetPeople();
            var expected = new[] { 40, 29, 25 };

            var result = people.OrderByDescending(x => x.Age).Select(x => x.Age);

            BetterAssert.AreEqual(expected, result);
        }
예제 #8
0
        public void WhichOneShouldIUse()
        {
            var methodResult = GetStudents().SelectMany(kid => kid.Subjects, (kid, subject) => kid.Name + " has " + subject);

            var queryResult = from kid in GetStudents()
                              from subject in kid.Subjects
                              select kid.Name + " has " + subject;

            // They are the same, so write the one that is easier to read!
            BetterAssert.AreEqual(methodResult, queryResult);
        }
예제 #9
0
        public void AnExampleOfConcat()
        {
            var first  = Enumerable.Range(1, 5);
            var second = Enumerable.Range(6, 5);

            var expected = Enumerable.Range(1, 10);

            var result = first.Concat(second);

            BetterAssert.AreEqual(expected, result);
        }
예제 #10
0
        public void AnExampleOfExcept()
        {
            var first  = Enumerable.Range(1, 5);
            var second = Enumerable.Range(4, 7);

            var expected = new[] { 1, 2, 3 };

            var result = first.Except(second);

            BetterAssert.AreEqual(expected, result);
        }
예제 #11
0
        public void AnExample()
        {
            var expected = new List <int>()
            {
                1, 2, 3, 4, 5
            };

            var result = numbers.Where(number => number < 6);

            BetterAssert.AreEqual(expected, result);
        }
예제 #12
0
        public void AnExample()
        {
            var expected = new List <string>()
            {
                "Color: Red", "Color: Green", "Color: Blue", "Color: Yellow", "Color: Black"
            };

            var result = colors.Select(color => "Color: " + color);

            BetterAssert.AreEqual(expected, result);
        }
        public void TheSameExampleOfSelectManyInQuerySyntax()
        {
            var list     = new[] { "1,2,3", "4,5", "6,7,8" };
            var expected = new[] { "1", "2", "3", "4", "5", "6", "7", "8" };

            var result = from item in list
                         from number in item.Split(',')
                         select number;

            BetterAssert.AreEqual(expected, result);
        }
예제 #14
0
        public void QuerySyntax()
        {
            var expected = new List <int>()
            {
                1, 2, 3, 4, 5
            };

            var result = from number in numbers
                         where number <= 5
                         select number;

            BetterAssert.AreEqual(expected, result);
        }
        public void TheSameExampleOfJoinWithMethodSyntax()
        {
            var expected = new[] { new { CourseName = "Math", PeriodName = "Fall" },
                                   new { CourseName = "History", PeriodName = "Fall" },
                                   new { CourseName = "Math", PeriodName = "Spring" },
                                   new { CourseName = "Geography", PeriodName = "Spring" } };

            var results = GetCourses().Join(GetMarkingPeriods(), course => course.SemesterId,
                                            period => period.SemesterId,
                                            (course, period) => new { course.CourseName, PeriodName = period.SemesterName }
                                            );

            BetterAssert.AreEqual(expected, results);
        }
        public void AnExampleOfJoin()
        {
            var expected = new [] { new { CourseName = "Math", PeriodName = "Fall" },
                                    new { CourseName = "History", PeriodName = "Fall" },
                                    new { CourseName = "Math", PeriodName = "Spring" },
                                    new { CourseName = "Geography", PeriodName = "Spring" } };

            var results = from course in GetCourses()
                          join period in GetMarkingPeriods()
                          on course.SemesterId equals period.SemesterId
                          select new { course.CourseName, PeriodName = period.SemesterName };

            BetterAssert.AreEqual(expected, results);
        }
예제 #17
0
        public void MethodSyntax()
        {
            var expected = new List <int>()
            {
                1, 2, 3, 4, 5
            };

            // This would more accurately match the above query, but the Select is redundant if you're writing it yourself
            //var result = numbers.Where(number => number <= 5).Select(number => number);

            var result = numbers.Where(number => number <= 5);

            BetterAssert.AreEqual(expected, result);
        }
예제 #18
0
        public void WithoutLinq()
        {
            var expected = new List <string>()
            {
                "Color: Red", "Color: Green", "Color: Blue", "Color: Yellow", "Color: Black"
            };

            var result = new List <string>();

            foreach (var color in colors)
            {
                result.Add("Color: " + color);
            }

            BetterAssert.AreEqual(expected, result);
        }
        public void AnExampleOfSelectManyWithoutLinq()
        {
            var list     = new[] { "1,2,3", "4,5", "6,7,8" };
            var expected = new[] { "1", "2", "3", "4", "5", "6", "7", "8" };

            var result = new List <string>();

            foreach (var item in list)
            {
                foreach (var number in item.Split(','))
                {
                    result.Add(number);
                }
            }

            BetterAssert.AreEqual(expected, result);
        }
        public void AnExampleOfGroupBy()
        {
            var courses = GetCourses();

            var result = courses.GroupBy(x => x.CourseName);

            var expectedGroups = 3;

            Assert.AreEqual(expectedGroups, result.Count());

            var expectedKeys = new [] { "Math", "History", "Geography" };

            BetterAssert.AreEqual(expectedKeys, result.Select(x => x.Key));

            var expectedItemsInMathtGroup = 2;

            Assert.AreEqual(expectedItemsInMathtGroup, result.Single(x => x.Key == "Math").Count());
        }
예제 #21
0
        public void WhereWithoutLinq()
        {
            var expected = new List <int>()
            {
                1, 2, 3, 4, 5
            };

            var result = new List <int>();

            foreach (var number in numbers)
            {
                if (number < 6)
                {
                    result.Add(number);
                }
            }

            BetterAssert.AreEqual(expected, result);
        }