private FavoriteLiveSongList GetAnalysisPart(Song song) { var setSongService = new SetSongService(Ioc.GetInstance <ISetSongRepository>()); var analysisService = new AnalysisService(Ioc.GetInstance <IAnalysisRepository>()); var songService = new SongService(Ioc.GetInstance <ISongRepository>()); //Get all Analysis for that Song but in groups of SetSong var analysis = analysisService.GetAnalysisBySong(song.SongId).GroupBy(x => x.SetSongId); double highestRating = 0; double? rating = 0; List <Guid> setSongIds = new List <Guid>(); //If there are no analysis then there is nothing to see here if (analysis.Count() == 0) { return(null); } var fave = new FavoriteLiveSongList(SetSong.FromSong(song)); //If there are 1 or more analysis then we need to find out which is the highest ranked foreach (var a in analysis) { rating = a.Average(x => x.Rating); var setSongId = a.First().SetSongId; if (rating.HasValue && rating.Value > highestRating) { highestRating = rating.Value; fave.HighestRatedShows = new List <Show>(); var setSong = (SetSong)setSongService.GetSetSong(setSongId); var show = GetShowFromSetSong(setSongId); fave.HighestRatedShows.Add(show); } else if (rating.HasValue && rating.Value == highestRating) { var setSong = (SetSong)setSongService.GetSetSong(setSongId); var show = GetShowFromSetSong(setSongId); fave.HighestRatedShows.Add(show); } } fave.HighestRating = highestRating; return(fave); }