private static void ImportGrammars(Lesson newLesson) { List <Grammar> grammarsToInsert = new List <Grammar>(); List <Grammar> grammarsToUpdate = new List <Grammar>(); List <Grammar> grammarsToDelete = new List <Grammar>(); Lesson oldLesson = database.Lessons.FirstOrDefault(x => x.Id == newLesson.Id); //lesson doesnt exists, so insert it if (oldLesson == null) { grammarsToInsert.AddRange(newLesson.Grammars); database.Add(newLesson); } else { //first find out wich Grammars to delete and wich to update foreach (Grammar dbGrammar in oldLesson.Grammars) { //to do so, find all Grammars in the database that are no longer in the new imported lesson Grammar newGrammar = newLesson.Grammars.FirstOrDefault(x => x.Id == dbGrammar.Id); //if the Grammar can't be found in the importLesson, than delete it if (newGrammar == null) { grammarsToDelete.Add(dbGrammar); } else //if its still in the list, than update the Grammar { dbGrammar.Fill(newGrammar); grammarsToUpdate.Add(dbGrammar); } } //than go through all Grammars from import lesson foreach (Grammar newGrammar in newLesson.Grammars) { //and find all Grammars that are not in the database Grammar dbGrammar = oldLesson.Grammars.FirstOrDefault(x => x.Id == newGrammar.Id); //if the Grammar isn't in the database than insert it new if (dbGrammar == null) { grammarsToInsert.Add(newGrammar); } } //update lesson oldLesson.Fill(newLesson); database.Update(oldLesson); } database.AddRange(grammarsToInsert); database.UpdateRange(grammarsToUpdate); database.DeleteRange(grammarsToDelete); }
private static void ImportKanjis(Lesson newLesson) { List <Kanji> kanjisToInsert = new List <Kanji>(); List <Kanji> kanjisToUpdate = new List <Kanji>(); List <Kanji> kanjisToDelete = new List <Kanji>(); Lesson oldLesson = database.Lessons.FirstOrDefault(x => x.Id == newLesson.Id); //lesson doesnt exists, so insert it if (oldLesson == null) { kanjisToInsert.AddRange(newLesson.Kanjis); database.Add(newLesson); } else { //first find out wich Kanjis to delete and wich to update foreach (Kanji dbKanji in oldLesson.Kanjis) { //to do so, find all Kanjis in the database that are no longer in the new imported lesson Kanji newKanji = newLesson.Kanjis.FirstOrDefault(x => x.Id == dbKanji.Id); //if the Kanji can't be found in the importLesson, than delete it if (newKanji == null) { kanjisToDelete.Add(dbKanji); } else //if its still in the list, than update the Kanji { dbKanji.Fill(newKanji); kanjisToUpdate.Add(dbKanji); } } //than go through all Kanjis from import lesson foreach (Kanji newKanji in newLesson.Kanjis) { //and find all Kanjis that are not in the database Kanji dbKanji = oldLesson.Kanjis.FirstOrDefault(x => x.Id == newKanji.Id); //if the Kanji isn't in the database than insert it new if (dbKanji == null) { kanjisToInsert.Add(newKanji); } } //update lesson oldLesson.Fill(newLesson); database.Update(oldLesson); } database.AddRange(kanjisToInsert); database.UpdateRange(kanjisToUpdate); database.DeleteRange(kanjisToDelete); }
private static void ImportWords(Lesson newLesson) { List <Word> wordsToInsert = new List <Word>(); List <Word> wordsToUpdate = new List <Word>(); List <Word> wordsToDelete = new List <Word>(); Lesson oldLesson = database.Lessons.FirstOrDefault(x => x.Id == newLesson.Id); //lesson doesnt exists, so insert it if (oldLesson == null) { wordsToInsert.AddRange(newLesson.Words); database.Add(newLesson); } else { //first find out wich words to delete and wich to update foreach (Word dbWord in oldLesson.Words) { //to do so, find all words in the database that are no longer in the new imported lesson Word newWord = newLesson.Words.FirstOrDefault(x => x.Id == dbWord.Id); //if the word can't be found in the importLesson, than delete it if (newWord == null) { wordsToDelete.Add(dbWord); } else //if its still in the list, than update the word { dbWord.Fill(newWord); wordsToUpdate.Add(dbWord); } } //than go through all words from import lesson foreach (Word newWord in newLesson.Words) { //and find all words that are not in the database Word dbWord = oldLesson.Words.FirstOrDefault(x => x.Id == newWord.Id); //if the word isn't in the database than insert it new if (dbWord == null) { wordsToInsert.Add(newWord); } } //update lesson oldLesson.Fill(newLesson); database.Update(oldLesson); } database.AddRange(wordsToInsert); database.UpdateRange(wordsToUpdate); database.DeleteRange(wordsToDelete); }