//filter the list of athletes by agegroup, gender, and range
        protected List <Triathlete> GetFilteredAthletes(List <Triathlete> allAthletes,
                                                        RaceFilterViewModel filter,
                                                        string sort   = null,
                                                        string search = null)
        {
            try
            {
                Trace.TraceInformation("GetFilteredAthletes-1");
                var selectedAgeGroupIds = AgeGroup.Expand(filter.SelectedAgeGroupIds);
                var selectedGenderIds   = Gender.Expand(filter.SelectedGenderIds);

                var athletes = allAthletes.Where(
                    a => selectedAgeGroupIds.Contains(a.RequestContext.AgeGroupId) &&
                    selectedGenderIds.Contains(a.RequestContext.GenderId));

                Trace.TraceInformation("GetFilteredAthletes-1a");


                if (!string.IsNullOrEmpty(sort))
                {
                    athletes = athletes.AsQueryable().OrderBy(sort);
                }
                Trace.TraceInformation("GetFilteredAthletes-1b");

                if (!String.IsNullOrEmpty(search))
                {
                    Trace.TraceInformation("GetFilteredAthletes-Search");

                    athletes = athletes.Where(a => a.Name.ToLower().Contains(search.ToLower())).ToList();
                }

                Trace.TraceInformation(String.Format("GetFilteredAthletes-2 athlete count {0}", athletes.Count()));

                var list = athletes.ToList();

                Trace.TraceInformation("GetFilteredAthletes-3");


                var result = new BasicFilterProvider(list, filter).GetAthletes();

                return(result);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                throw ex;
            }
        }
        protected override ActionResult DisplayResultsView(RaceFilterViewModel filter)
        {
            var viewModel = new AgeGroupCompareViewModel();

            viewModel.Filter = filter;

            List <Triathlete> allAthletes = GetAllAthletesForRaces(filter);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var selectedGenderIds = Gender.Expand(filter.SelectedGenderIds);

            var resultingAthletes = new List <Triathlete>();

            //calculating each selected age groups so that we can do the same when we draw the chart
            foreach (var agId in AgeGroup.Expand(viewModel.Filter.SelectedAgeGroupIds)) //collect the stats for each age group
            {
                var athletesPerAG = allAthletes.Where(
                    a => a.RequestContext.AgeGroupId == agId &&
                    selectedGenderIds.Contains(a.RequestContext.GenderId)).ToList();

                var filteredAthletes = new BasicFilterProvider(athletesPerAG, filter).GetAthletes();

                resultingAthletes.AddRange(filteredAthletes);

                var stats = GetStats(filteredAthletes);
                stats.AgeGroupId = agId;
                viewModel.Stats.Add(stats);
            }

            Trace.TraceInformation("AgeGroupCompare Calulating all took: " + stopwatch.Elapsed);
            stopwatch.Stop();

            viewModel.Triathletes = resultingAthletes;
            return(View("Compare", viewModel));
        }