Exemplo n.º 1
0
        public void it_uses_aggregation()
        {
            // Arrange
            var carReader     = new CarReader();
            var manufacturers = carReader.ProcessManufacturers("TestAssets/manufacturers.csv");
            var cars          = carReader.ProcessCars("TestAssets/fuel.csv");

            var query = cars
                        .GroupBy(c => c.Manufacturer)
                        .Select(g =>
            {
                var results = g.Aggregate(new CarStatistics(),
                                          (acc, c) => acc.Accumulate(c),
                                          acc => acc.Compute());

                return(new
                {
                    Name = g.Key,
                    Avg = results.Average,
                    Max = results.Max,
                    Min = results.Min
                });
            })
                        .OrderByDescending(r => r.Max);


            foreach (var result in query)
            {
                _testOutputHelper.WriteLine($"{result.Name}");
                _testOutputHelper.WriteLine($"\t Max : {result.Max}");
                _testOutputHelper.WriteLine($"\t Min : {result.Min}");
                _testOutputHelper.WriteLine($"\t Avg : {result.Avg}");
            }
        }
Exemplo n.º 2
0
        public void it_uses_group_join()
        {
            // Arrange
            var carReader     = new CarReader();
            var manufacturers = carReader.ProcessManufacturers("TestAssets/manufacturers.csv");
            var cars          = carReader.ProcessCars("TestAssets/fuel.csv");

            var query = manufacturers
                        .GroupJoin(cars,
                                   m => m.Name,
                                   c => c.Manufacturer,
                                   (m, g) =>
                                   new
            {
                Manufacturer = m,
                Cars         = g
            }
                                   )
                        .OrderBy(m => m.Manufacturer.Name);

            foreach (var group in query)
            {
                _testOutputHelper.WriteLine($"{group.Manufacturer.Name} : {group.Manufacturer.Headquarters}");

                foreach (var car in group.Cars.OrderByDescending(g => g.Combined).Take(2))
                {
                    _testOutputHelper.WriteLine($"\t{car.Name} : {car.Combined}");
                }
            }
        }
Exemplo n.º 3
0
        public void it_uses_join_with_multiple_keys_to_combine_car_and_manufacturer()
        {
            // Arrange
            var carReader     = new CarReader();
            var manufacturers = carReader.ProcessManufacturers("TestAssets/manufacturers.csv");

            var cars = carReader
                       .ProcessCars("TestAssets/fuel.csv")
                       .Join(manufacturers,
                             c => new { c.Manufacturer, c.Year },
                             m => new { Manufacturer = m.Name, m.Year },
                             (car, manufacturer) =>
                             new
            {
                car.Manufacturer,
                car.Name,
                car.Combined,
                manufacturer.Headquarters
            })
                       .OrderByDescending(car => car.Combined)
                       .ThenBy(car => car.Name)
                       .Select(car => new
            {
                car.Headquarters,
                car.Name,
                car.Combined
            });

            foreach (var car in cars)
            {
                _testOutputHelper.WriteLine($"{car.Headquarters} - {car.Name} : {car.Combined}");
            }
        }
Exemplo n.º 4
0
        public void it_group_cars_by_manufacturer_and_shows_two_most_efficient_cars()
        {
            // Arrange
            var carReader     = new CarReader();
            var manufacturers = carReader.ProcessManufacturers("TestAssets/manufacturers.csv");

            var query = carReader
                        .ProcessCars("TestAssets/fuel.csv")
                        .GroupBy(car => car.Manufacturer.ToUpper())
                        .OrderBy(group => group.Key);

            foreach (var group in query)
            {
                _testOutputHelper.WriteLine($"{group.Key} has {group.Count()} cars");

                foreach (var car in group.OrderByDescending(g => g.Combined).Take(2))
                {
                    _testOutputHelper.WriteLine($"\t{car.Name} : {car.Combined}");
                }
            }
        }