/// <summary> /// The get or create pitches in db. /// </summary> /// <param name="pitches"> /// The pitches. /// </param> /// <returns> /// The <see cref="T:int[]"/>. /// </returns> private int[] GetOrCreatePitchesInDb(List <Pitch> pitches) { var newPitches = new List <LibiadaWeb.Pitch>(); var result = new LibiadaWeb.Pitch[pitches.Count]; int[] midiNumbers = pitches.Select(p => p.MidiNumber).ToArray(); Dictionary <int, LibiadaWeb.Pitch> existingPitches = db.Pitch.Where(p => midiNumbers.Contains(p.Midinumber)) .ToDictionary(p => p.Midinumber); for (int i = 0; i < pitches.Count; i++) { Pitch pitch = pitches[i]; if (existingPitches.ContainsKey(pitch.MidiNumber)) { result[i] = existingPitches[pitch.MidiNumber]; if (pitch.Alter != result[i].Accidental || pitch.Step != result[i].NoteSymbol || pitch.Octave != result[i].Octave) { throw new Exception("Found in db pitch is not equal to the local pitch."); } } else { result[i] = new LibiadaWeb.Pitch { Accidental = pitch.Alter, NoteSymbol = pitch.Step, Octave = pitch.Octave, Midinumber = pitch.MidiNumber }; newPitches.Add(result[i]); } } db.Pitch.AddRange(newPitches); db.SaveChanges(); return(result.Select(p => p.Id).ToArray()); }
/// <summary> /// The get or create pitches in db. /// </summary> /// <param name="pitches"> /// The pitches. /// </param> /// <returns> /// The <see cref="T:int[]"/>. /// </returns> private int[] GetOrCreatePitchesInDb(List<Pitch> pitches) { var newPitches = new List<LibiadaWeb.Pitch>(); var result = new int[pitches.Count]; for (int i = 0; i < pitches.Count; i++) { var pitch = pitches[i]; var noteSymbol = pitch.Step.ToString(); var accidental = pitch.Alter.ToString(); if (db.Pitch.Any(p => p.Midinumber == pitch.MidiNumber)) { var databasePitch = db.Pitch.Include(p => p.NoteSymbol).Include(p => p.Accidental).Single(p => p.Midinumber == pitch.MidiNumber); result[i] = databasePitch.Id; if (pitch.Alter.ToString() != databasePitch.Accidental.Name || pitch.Step.ToString() != databasePitch.NoteSymbol.Name || pitch.Octave != databasePitch.Octave) { throw new Exception("Found in db pitch not equals to local pitch."); } } else { var newPitch = new LibiadaWeb.Pitch { AccidentalId = db.Accidental.Single(a => a.Name == accidental).Id, NoteSymbolId = db.NoteSymbol.Single(n => n.Name == noteSymbol).Id, Octave = pitch.Octave, Midinumber = pitch.MidiNumber }; newPitches.Add(newPitch); result[i] = newPitch.Id; } } db.Pitch.AddRange(newPitches); db.SaveChanges(); return result; }