public MusicRepository(ApplicationDbContext context) { this.context = context; // If MusicTracks is empty, load all data from DB if (MusicTracks.Count == 0) { // load genre/instrument/mood/musicTrack list from db genreList.AddRange(context.GenreTags.OrderBy(g => g.Tag).ToList()); // THE FAST WAY moodList.AddRange(context.MoodTags.OrderBy(m => m.Tag).ToList()); // THE SLOW WAY //List<MoodTag> allMoodTags = context.MoodTags.ToList(); //foreach (MoodTag mt in allMoodTags) //{ // moodList.Add(mt.Tag); //} //moodList.Sort(); instrumentList.AddRange(context.InstrumentTags.OrderBy(i => i.Tag).ToList()); musicTrackInstrumentTags.AddRange(context.MusicTrackInstrumentTags.ToList()); musicTrackMoodTags.AddRange(context.MusicTrackMoodTags.ToList()); musicTracks = context.MusicTracks.Include(m => m.Genre).Include(x => x.MusicTrackMoodTags).Include(x => x.MusicTrackInstrumentTags).ToList(); foreach (MusicTrack musicTrack in musicTracks) { foreach (MusicTrackMoodTag mt in musicTrack.MusicTrackMoodTags) { musicTrack.Moods.Add(MoodList.First(m => m.ID == mt.MoodTagID)); } foreach (MusicTrackInstrumentTag it in musicTrack.MusicTrackInstrumentTags) { musicTrack.Instruments.Add(InstrumentList.First(m => m.ID == it.InstrumentTagID)); } } } }
public void RefreshMusicTracks() { musicTracks = new List <MusicTrack>(); musicTracks = context.MusicTracks.Include(m => m.Genre).Include(x => x.MusicTrackMoodTags).Include(x => x.MusicTrackInstrumentTags).ToList(); foreach (MusicTrack musicTrack in musicTracks) { foreach (MusicTrackMoodTag mt in musicTrack.MusicTrackMoodTags) { musicTrack.Moods.Add(MoodList.First(m => m.ID == mt.MoodTagID)); } foreach (MusicTrackInstrumentTag it in musicTrack.MusicTrackInstrumentTags) { musicTrack.Instruments.Add(InstrumentList.First(m => m.ID == it.InstrumentTagID)); } } }