/// <summary> /// Get a list of all vehicles from the Star Wars API and present some summary information. /// The top portion of the view will display the total number of vehicles where the cost is not "unknown", /// as well as the total number of unique manufacturers. /// For the table, take the vehicles where the cost is not "unknown", and group them by manufacturer. /// For each group(table row), get the manufacturer name, the number of vehicles by that manufacturer, /// and their average cost. /// Sort them by vehicle count(highest to lowest), then by average cost(highest to lowest). /// </summary> /// <returns>ActionResult</returns> public async Task <IActionResult> VehicleSummary() { var model = new VehicleSummaryViewModel(); var vehicles = await _StarWarsService.GetVehiclesAsync(); var vehiclesWithKnownCost = vehicles.Where(v => v.Cost != "unknown").ToList(); model.VehicleCount = vehiclesWithKnownCost.Count; model.ManufacturerCount = vehicles.Select(v => v.Manufacturer).Distinct().Count(); model.Details = vehiclesWithKnownCost .Select(v => new VehicleViewModel { Cost = v.Cost, Manufacturer = v.Manufacturer }) .GroupBy(v => v.Manufacturer) .Select(g => new VehicleStatsViewModel { ManufacturerName = g.Key, VehicleCount = g.Count(), AverageCost = g.Average(v => v.CostAsDouble) }) .OrderByDescending(s => s.VehicleCount) .ThenByDescending(s => s.AverageCost) .ToList(); return(View(model)); }