/// <summary> /// The get or create notes in db. /// </summary> /// <param name="alphabet"> /// The alphabet. /// </param> /// <returns> /// The <see cref="T:long[]"/>. /// </returns> public long[] GetOrCreateNotesInDb(Alphabet alphabet) { var newNotes = new List <Note>(); var result = new Note[alphabet.Cardinality]; ValueNote[] notesAlphabet = alphabet.Cast <ValueNote>().ToArray(); string[] stringNotes = notesAlphabet.Select(n => n.ToString()).ToArray(); Dictionary <string, Note> existingNotes = db.Note.Where(n => stringNotes.Contains(n.Value)) .ToDictionary(n => n.Value); for (int i = 0; i < notesAlphabet.Length; i++) { ValueNote note = notesAlphabet[i]; int[] pitches = GetOrCreatePitchesInDb(note.Pitches); string localStringNote = stringNotes[i]; if (existingNotes.ContainsKey(localStringNote)) { result[i] = existingNotes[localStringNote]; if (note.Triplet != result[i].Triplet || note.Duration.Denominator != result[i].Denominator || note.Duration.Numerator != result[i].Numerator || note.Tie != result[i].Tie) { throw new Exception("Found in db note is not equal to local note."); } } else { result[i] = new Note { Value = localStringNote, Triplet = note.Triplet, Denominator = note.Duration.Denominator, Numerator = note.Duration.Numerator, Tie = note.Tie, Pitch = db.Pitch.Where(p => pitches.Contains(p.Id)).ToList(), Notation = Notation.Notes }; newNotes.Add(result[i]); } } db.Note.AddRange(newNotes); db.SaveChanges(); return(result.Select(n => n.Id).ToArray()); }