private WeatherInfo AggregateInternal(List<WeatherInfo> weatherInfos) { var weatherInfo = new WeatherInfo(); var existingWeatherInfos = weatherInfos.Where(p => p != null).ToList(); weatherInfo.Country = existingWeatherInfos.GetOneResult(info => info.Country != null, info => info.Country); weatherInfo.Description = existingWeatherInfos.GetOneResult(info => info.Description != null, info => info.Description); weatherInfo.Elevation = existingWeatherInfos.GetOneResult(info => info.Elevation != null, info => info.Elevation); weatherInfo.RelativeHumidity = existingWeatherInfos.GetOneResult(info => info.RelativeHumidity != null, info => info.RelativeHumidity); weatherInfo.WindDirection = existingWeatherInfos.GetOneResult(info => info.WindDirection != null, info => info.WindDirection); var latititudes = existingWeatherInfos.GetMultipleResults(p => p.Latitude.HasValue, p => p.Latitude); weatherInfo.Latitude = latititudes.Any() ? latititudes.Average() : null; var longitudes = existingWeatherInfos.GetMultipleResults(p => p.Longitude.HasValue, p => p.Longitude); weatherInfo.Longitude = longitudes.Any() ? longitudes.Average() : null; var pressuresInMb = existingWeatherInfos.GetMultipleResults(p => p.PressureMb.HasValue, p => p.PressureMb); weatherInfo.PressureMb = pressuresInMb.Any() ? MathExtensions.Floor(pressuresInMb.Average()) : null; var temperaturesCelcius = existingWeatherInfos.GetMultipleResults(p => p.TemperatureCelcius.HasValue, p => p.TemperatureCelcius); weatherInfo.TemperatureCelcius = temperaturesCelcius.Any() ? temperaturesCelcius.Average() : null; var visibilityDistances = existingWeatherInfos.GetMultipleResults(p => p.VisibilityDistance.HasValue, p => p.VisibilityDistance); weatherInfo.VisibilityDistance = visibilityDistances.Any() ? visibilityDistances.Average() : null; var windAngles = existingWeatherInfos.GetMultipleResults(p => p.WindAngle.HasValue, p => p.WindAngle); weatherInfo.WindAngle = windAngles.Any() ? MathExtensions.Floor(windAngles.Average()) : null; var windSpeedsKph = existingWeatherInfos.GetMultipleResults(p => p.WindSpeedKph.HasValue, p => p.WindSpeedKph); weatherInfo.WindSpeedKph = windSpeedsKph.Any() ? windSpeedsKph.Average() : null; var windSpeedsMs = existingWeatherInfos.GetMultipleResults(p => p.WindSpeedMs.HasValue, p => p.WindSpeedMs); weatherInfo.WindSpeedMs = windSpeedsMs.Any() ? windSpeedsMs.Average() : null; return weatherInfo; }
private void TestAverageValues(WeatherInfo aggregatedWeatherInfo) { Assert.AreEqual((Latitude1 + Latitude2)/2, aggregatedWeatherInfo.Latitude, delta); Assert.AreEqual((Longitude1 + Longitude2)/2, aggregatedWeatherInfo.Longitude, delta); Assert.AreEqual((PressureMb1 + PressureMb2)/2, aggregatedWeatherInfo.PressureMb, delta); Assert.AreEqual((TemperatureCelcius1 + TemperatureCelcius2)/2, aggregatedWeatherInfo.TemperatureCelcius, delta); Assert.AreEqual((VisibilityDistance1 + VisibilityDistance2)/2, aggregatedWeatherInfo.VisibilityDistance, delta); Assert.AreEqual((WindAngle1 + WindAngle2)/2, aggregatedWeatherInfo.WindAngle, delta); Assert.AreEqual((WindSpeedKph1 + WindSpeedKph2)/2, aggregatedWeatherInfo.WindSpeedKph, delta); Assert.AreEqual((WindSpeedMs1 + WindSpeedMs2)/2, aggregatedWeatherInfo.WindSpeedMs, delta); }