private static void ImportClozes(Lesson importLesson) { Lesson dbLesson = database.lessons.FirstOrDefault(x => x.id == importLesson.id); //lesson exists, so update it if (dbLesson != null) { //update lesson properties dbLesson.Fill(importLesson); //first find out wich clozes to delete List <Cloze> clozesToDelete = new List <Cloze>(); foreach (Cloze dbCloze in dbLesson.Clozes) { //to do so, find all clozes in the database that are no longer in the new imported lesson Cloze cloze = importLesson.Clozes.FirstOrDefault(x => x.id == dbCloze.id); //if the cloze can't be found in the importLesson, than delete it if (cloze == null) { clozesToDelete.Add(dbCloze); } else //if its still in the list, than update the cloze { dbCloze.Fill(cloze); } } //than go through all clozes from import lesson foreach (Cloze cloze in importLesson.Clozes) { //and find all clozes that are not in the database Cloze dbCloze = dbLesson.Clozes.FirstOrDefault(x => x.id == cloze.id); //if the cloze isn't in the database than insert it new if (dbCloze == null) { cloze.Lesson = dbLesson; dbLesson.Clozes.Add(cloze); database.clozes.InsertOnSubmit(cloze); } } database.clozes.DeleteAllOnSubmit(clozesToDelete); } else //lesson does not exists, so just insert it { database.clozes.InsertAllOnSubmit(importLesson.Clozes); database.lessons.InsertOnSubmit(importLesson); } }
private static void ImportKanjis(Lesson importLesson) { Lesson dbLesson = database.lessons.FirstOrDefault(x => x.id == importLesson.id); //lesson exists, so update it if (dbLesson != null) { //update lesson properties dbLesson.Fill(importLesson); //first find out wich kanjis to delete List <Kanji> kanjisToDelete = new List <Kanji>(); foreach (Kanji dbKanji in dbLesson.Kanjis) { //to do so, find all kanjis in the database that are no longer in the new imported lesson Kanji kanji = importLesson.Kanjis.FirstOrDefault(x => x.id == dbKanji.id); //if the kanji can't be found in the importLesson, than delete it if (kanji == null) { kanjisToDelete.Add(dbKanji); } else //if its still in the list, than update the kanji { dbKanji.Fill(kanji); } } //than go through all kanjis from import lesson foreach (Kanji kanji in importLesson.Kanjis) { //and find all kanjis that are not in the database Kanji dbKanji = dbLesson.Kanjis.FirstOrDefault(x => x.id == kanji.id); //if the kanji isn't in the database than insert it new if (dbKanji == null) { kanji.Lesson = dbLesson; dbLesson.Kanjis.Add(kanji); database.kanjis.InsertOnSubmit(kanji); } } database.kanjis.DeleteAllOnSubmit(kanjisToDelete); } else //lesson does not exists, so just insert it { database.kanjis.InsertAllOnSubmit(importLesson.Kanjis); database.lessons.InsertOnSubmit(importLesson); } }
private static void ImportWords(Lesson importLesson) { Lesson dbLesson = database.lessons.FirstOrDefault(x => x.id == importLesson.id); //lesson exists, so update it if (dbLesson != null) { //update lesson properties dbLesson.Fill(importLesson); //first find out wich words to delete List <Word> wordsToDelete = new List <Word>(); foreach (Word dbWord in dbLesson.Words) { //to do so, find all words in the database that are no longer in the new imported lesson Word word = importLesson.Words.FirstOrDefault(x => x.id == dbWord.id); //if the word can't be found in the importLesson, than delete it if (word == null) { wordsToDelete.Add(dbWord); } else //if its still in the list, than update the word { dbWord.Fill(word); } } //than go through all words from import lesson foreach (Word word in importLesson.Words) { //and find all words that are not in the database Word dbWord = dbLesson.Words.FirstOrDefault(x => x.id == word.id); //if the word isn't in the database than insert it new if (dbWord == null) { word.Lesson = dbLesson; dbLesson.Words.Add(word); database.words.InsertOnSubmit(word); } } database.words.DeleteAllOnSubmit(wordsToDelete); } else //lesson does not exists, so just insert it { database.words.InsertAllOnSubmit(importLesson.Words); database.lessons.InsertOnSubmit(importLesson); } }