public void CalculatePlayerHoleStatistics(int month, int year) { Check.Argument.IsNotZeroOrNegative(month, "month"); Check.Argument.IsNotZeroOrNegative(year, "year"); DeletePlayerHoleStatistics(month, year); var players = _playerRepository.All().ToList(); var shots = _shotRepository.GetByMonthAndYear(month, year); var holes = _holeRepository.All().ToList(); foreach (var player in players) { var playerShots = shots.Where(s => s.Player.Id == player.Id); if (playerShots.Any()) { foreach (var hole in holes) { var playerHoleShots = playerShots.Where(s => s.Hole.Id == hole.Id); if (playerHoleShots.Any()) { var playerHoleStatistics = new PlayerHoleStatistics() { Player = player, Hole = hole, Month = month, Year = year }; playerHoleStatistics.ShotsMade = playerHoleShots.Count(s => s.ShotMade); playerHoleStatistics.Attempts = playerHoleShots.Sum(s => s.Attempts); playerHoleStatistics.ShootingPercentage = Decimal.Round((decimal)playerHoleStatistics.ShotsMade / (decimal)playerHoleStatistics.Attempts, 3, MidpointRounding.AwayFromZero); playerHoleStatistics.PointsScored = playerHoleShots.Where(s => s.ShotType.Id != ShotTypePush).Sum(s => s.Points); playerHoleStatistics.Pushes = playerHoleShots.Count(s => s.ShotType.Id == ShotTypePush); playerHoleStatistics.Steals = playerHoleShots.Count(s => s.ShotType.Id == ShotTypeSteal); playerHoleStatistics.SugarFreeSteals = playerHoleShots.Count(s => s.ShotType.Id == ShotTypeSugarFreeSteal); _playerHoleStatisticsRepository.Add(playerHoleStatistics); } } } } }