private void SaveUnit(object sender, RoutedEventArgs e) { if (UnitList.SelectedItem != null) { int unitId = (UnitList.SelectedItem as UnitPropertis).UnitId; ShopContext shopContext = new ShopContext(); float userStep = Convert.ToSingle(UnitStep.Text); float step = (float)shopContext.Unit.Where(u => u.UnitID == unitId).Select(u => u.UnitStep).First(); if (userStep != step) { Unit editedUnit = shopContext.Unit.Where(u => u.UnitID == unitId).First(); editedUnit.UnitStep = userStep; } List <UnitDictionary> DBNames = shopContext.UnitDictionary .Where(d => d.UnitID == unitId).ToList(); List <UnitDictionary> dictionaryList = new List <UnitDictionary>(); for (int i = 0; i < UnitNames.Items.Count; i++) { ContentPresenter c = (ContentPresenter)UnitNames.ItemContainerGenerator.ContainerFromItem(UnitNames.Items[i]); ComboBox comboBox = c.ContentTemplate.FindName("UnitLanguage", c) as ComboBox; TextBox text = c.ContentTemplate.FindName("UnitName", c) as TextBox; UnitDictionary dictionary = new UnitDictionary(); if (text.Text == "") { continue; } else { dictionary.Name = text.Text; } if (comboBox.SelectedItem == null) { continue; } else { string userLang = comboBox.SelectedItem.ToString(); int userLangId = shopContext.Language.Where(l => l.Name == userLang).Select(l => l.LanguageID).First(); dictionary.LanguageID = userLangId; dictionary.UnitID = unitId; } dictionaryList.Add(dictionary); } List <UnitDictionary> duplicates = dictionaryList.GroupBy(d => d.LanguageID).SelectMany(d => d.Skip(1)).ToList(); if (duplicates.Count != 0) { MessageBox.Show("Duplicated languages!"); return; } if (dictionaryList.Count > 0) { shopContext.UnitDictionary.RemoveRange(DBNames); shopContext.UnitDictionary.AddRange(dictionaryList); shopContext.SaveChanges(); } else { MessageBox.Show("Some fields is empty!"); return; } RefreshList(); } }
private void SaveUnit(object sender, RoutedEventArgs e) { if (UnitStep.Text == "") { MessageBox.Show("Unit step is empty!"); return; } ShopContext shopContext = new ShopContext(); Unit unit = new Unit(); unit.UnitStep = Convert.ToSingle(UnitStep.Text); shopContext.Unit.Add(unit); List <UnitDictionary> dictionaryList = new List <UnitDictionary>(); for (int i = 0; i < UnitNames.Items.Count; i++) { ContentPresenter c = (ContentPresenter)UnitNames.ItemContainerGenerator.ContainerFromItem(UnitNames.Items[i]); ComboBox comboBox = c.ContentTemplate.FindName("NewUnitLanguage", c) as ComboBox; TextBox text = c.ContentTemplate.FindName("NewUnitName", c) as TextBox; UnitDictionary dictionary = new UnitDictionary(); if (text.Text == "") { continue; } else { dictionary.Name = text.Text; } if (comboBox.SelectedItem == null) { continue; } else { string lang = comboBox.SelectedItem.ToString(); int langId = shopContext.Language.Where(l => l.Name == lang).Select(l => l.LanguageID).First(); dictionary.LanguageID = langId; } dictionaryList.Add(dictionary); } List <UnitDictionary> duplicates = dictionaryList.GroupBy(d => d.LanguageID).SelectMany(d => d.Skip(1)).ToList(); if (duplicates.Count != 0) { MessageBox.Show("Duplicated languages!"); return; } foreach (var item in dictionaryList) { if (shopContext.UnitDictionary.Where(f => f.Name == item.Name && f.LanguageID == item.LanguageID).Count() > 0) { UnitDictionary unitDup = shopContext.UnitDictionary.Where(f => f.Name == item.Name && f.LanguageID == item.LanguageID).FirstOrDefault(); if (shopContext.Unit.Where(u => u.UnitID == unitDup.UnitID && u.UnitStep == unit.UnitStep).FirstOrDefault() != null) { MessageBox.Show("Unit is existed!"); return; } } } if (dictionaryList.Count > 0) { shopContext.SaveChanges(); } else { MessageBox.Show("Some fields is empty!"); return; } List <int> foodTypeIdList = shopContext.Unit.Where(u => u.UnitStep == unit.UnitStep).Select(u => u.UnitID).ToList(); foreach (var id in foodTypeIdList) { int idCount = shopContext.UnitDictionary.Where(u => u.UnitID == id).Count(); if (idCount == 0) { foreach (var item in dictionaryList) { item.UnitID = id; shopContext.UnitDictionary.Add(item); } break; } } shopContext.SaveChanges(); UnitStep.Text = ""; UnitNames.Items.Clear(); UnitNames.Items.Add(new NewLanguage { Languages = Languages }); }