//UPDATE public static int UpdateSubCategory(subcategories subCategory) { subcategories subCategoryToUpdate = GetSubCategoryById(subCategory.Id); subCategoryToUpdate.Name = subCategory.Name; subCategoryToUpdate.categories_Id = subCategory.categories_Id; subCategoryToUpdate.categories = subCategory.categories; subCategoryToUpdate.events = subCategory.events; int affectedRows; try { affectedRows = Context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { // Get entry DbEntityEntry entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; // Display or log error messages foreach (DbValidationError subItem in item.ValidationErrors) { string message = string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName); Console.WriteLine(message); } // Rollback changes switch (entry.State) { case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } } return affectedRows = 0; } Context.Entry(subCategoryToUpdate).Reload(); return affectedRows; }
//ADD public static bool AddSubCategory(subcategories sC) { Context.subcategories.Add(sC); try { Context.SaveChanges(); } catch (DbUpdateException dbEx) { return false; } catch (Exception e) { return false; } Context.Entry(sC).Reload(); return true; }
public static List<events> FilterEvents(DateTime? sT, DateTime? eT, communities c, associations a, categories cat, subcategories subCat, string word) { List<events> resultList = GetAllEvents(); if (sT != null) { resultList = resultList.Where(e => e.StartDate >= sT).ToList(); } if (eT != null) { resultList = resultList.Where(e => e.EndDate <= eT).ToList(); } if (c != null) { var preResultList = new List<events>(); preResultList.AddRange(resultList.Where(e => e.communities.Contains(c)).ToList()); List<events> list = resultList; foreach (var ev in c.associations.SelectMany(asso => list.Where(e => e.associations.Contains(asso)).Where(ev => !preResultList.Contains(ev)))) { preResultList.Add(ev); } resultList = preResultList; } if (a != null) { resultList = resultList.Where(e => e.associations.Contains(a)).ToList(); } if (cat != null) { List<events> eventsInAsso = new List<events>(); foreach (var asso in cat.associations) { eventsInAsso.AddRange(resultList.Where(e => e.associations.Contains(asso))); } resultList = eventsInAsso; } if (subCat != null) { resultList = resultList.Where(e => e.subcategories.Contains(subCat)).ToList(); } if (!String.IsNullOrWhiteSpace(word)) { resultList = GetEventsFromListBySearchWord(resultList, word); } return resultList; }