public async Task <IActionResult> AgeGroup(int year)
        {
            var race = await this.context.Races
                       .Include(x => x.AgeGroups)
                       .Include(x => x.RaceRunners)
                       .ThenInclude(x => x.Runner)
                       .Include(x => x.RaceRunners)
                       .ThenInclude(x => x.AgeGroup)
                       .Include(x => x.Results)
                       .ThenInclude(x => x.Runner)
                       .FirstAsync(x => x.Date.Year == year);

            var ageGroups = new List <AgeGroupResultsViewModel>();

            foreach (var ageGroup in race.AgeGroups)
            {
                var raceRunnerIds   = race.RaceRunners.Where(x => x.AgeGroup.Id == ageGroup.Id).Select(x => x.Runner.Id);
                var ageGroupResults = race.Results.Where(x => raceRunnerIds.Contains(x.Runner.Id));

                var results = new List <ResultViewModel>();

                foreach (var ageGroupResult in ageGroupResults)
                {
                    var res = new ResultViewModel
                    {
                        Place        = ageGroupResult.AgeGroupPlace,
                        Name         = $"{ageGroupResult.Runner.FirstName} {ageGroupResult.Runner.LastName}",
                        Gender       = ageGroupResult.Runner.Gender,
                        AgeGroupName = ageGroup.Name,
                        Time         = ageGroupResult.Time,
                        RaceName     = race.Name
                    };

                    results.Add(res);
                }

                var result = new AgeGroupResultsViewModel
                {
                    Name    = ageGroup.Name,
                    Gender  = ageGroup.Gender,
                    Results = results.OrderBy(x => x.Place).ToList()
                };

                ageGroups.Add(result);
            }

            var viewModel = new AgeGroupViewModel
            {
                Race      = race,
                AgeGroups = ageGroups.OrderBy(x => x.Gender).ThenBy(x => x.Name).ToList()
            };

            return(View(viewModel));
        }
        public async Task <ActionResult <AgeGroupRecordsViewModel> > AgeGroup()
        {
            var maleAgeGroups = await context.AgeGroups
                                .Where(x => x.Gender == Gender.Male)
                                .Select(x => new
            {
                Name   = x.Name,
                Gender = x.Gender
            })
                                .Distinct()
                                .ToListAsync();

            var femaleAgeGroups = await context.AgeGroups
                                  .Where(x => x.Gender == Gender.Female)
                                  .Select(x => new
            {
                Name   = x.Name,
                Gender = x.Gender
            })
                                  .Distinct()
                                  .ToListAsync();

            var viewModel = new AgeGroupRecordsViewModel();

            foreach (var ageGroup in maleAgeGroups)
            {
                var results = await context.Results
                              .Where(x => x.AgeGroup.Name == ageGroup.Name && x.Runner.Gender == ageGroup.Gender)
                              .OrderBy(x => x.Time)
                              .Take(10)
                              .Select(x => new ResultViewModel
                {
                    Name          = $"{x.Runner.FirstName} {x.Runner.LastName}",
                    Gender        = x.Runner.Gender,
                    RaceName      = x.Race.Name,
                    RaceYear      = x.Race.Date.Year,
                    Time          = x.Time,
                    AgeGroupPlace = x.AgeGroupPlace,
                    OverallPlace  = x.OverallPlace,
                    AgeGroupName  = x.Race.RaceRunners.Single(y => y.Runner.Id == x.Runner.Id).AgeGroup.Name
                })
                              .ToListAsync();

                var ageGroupResults = new AgeGroupResultsViewModel
                {
                    Name    = ageGroup.Name,
                    Gender  = ageGroup.Gender,
                    Results = results
                };

                viewModel.MaleAgeGroups.Add(ageGroupResults);
            }

            foreach (var ageGroup in femaleAgeGroups)
            {
                var results = await context.Results
                              .Where(x => x.AgeGroup.Name == ageGroup.Name && x.Runner.Gender == ageGroup.Gender)
                              .OrderBy(x => x.Time)
                              .Take(10)
                              .Select(x => new ResultViewModel
                {
                    Name          = $"{x.Runner.FirstName} {x.Runner.LastName}",
                    Gender        = x.Runner.Gender,
                    RaceName      = x.Race.Name,
                    RaceYear      = x.Race.Date.Year,
                    Time          = x.Time,
                    AgeGroupPlace = x.AgeGroupPlace,
                    OverallPlace  = x.OverallPlace,
                    AgeGroupName  = x.Race.RaceRunners.Single(y => y.Runner.Id == x.Runner.Id).AgeGroup.Name
                })
                              .ToListAsync();

                var ageGroupResults = new AgeGroupResultsViewModel
                {
                    Name    = ageGroup.Name,
                    Gender  = ageGroup.Gender,
                    Results = results
                };

                viewModel.FemaleAgeGroups.Add(ageGroupResults);
            }

            return(View(viewModel));
        }