public void AddElement(BindingIngridients model) { int maxId = 0; for (int i = 0; i < source.Ingridients.Count; ++i) { if (source.Ingridients[i].Id > maxId) { maxId = source.Ingridients[i].Id; } if (source.Ingridients[i].IngredientName == model.IngridientName) { throw new Exception("Уже есть компонент с таким названием"); } } source.Ingridients.Add(new Ingredient { Id = maxId + 1, IngredientName = model.IngridientName }); }
public void UpdElement(BindingIngridients model) { int index = -1; for (int i = 0; i < source.Ingridients.Count; ++i) { if (source.Ingridients[i].Id == model.Id) { index = i; } if (source.Ingridients[i].IngredientName == model.IngridientName && source.Ingridients[i].Id != model.Id) { throw new Exception("Уже есть компонент с таким названием"); } } if (index == -1) { throw new Exception("Элемент не найден"); } source.Ingridients[index].IngredientName = model.IngridientName; }