public Category ToCategory(WorkLinqToSql.CatalogCategory catalogCategory) { Category category = new Category(); category.ID = catalogCategory.Id; category.Name = catalogCategory.Name; return(category); }
public bool RemoveCatalogCategories(int idCatalogCategories) { try { CatalogDatabaseDataContext context = new WorkLinqToSql.CatalogDatabaseDataContext(); WorkLinqToSql.CatalogCategory category = (from c in context.CatalogCategories where c.Id == idCatalogCategories select c).Single <WorkLinqToSql.CatalogCategory>(); if (category.Name == "Others") { return(false); } var mandatSpecifications = from ms in context.MandatSpecificCatalogCategories where ms.CatalogCategoryID == category.Id select ms; foreach (var ms in mandatSpecifications) { context.MandatSpecificCatalogCategories.DeleteOnSubmit(ms); } context.MandatSpecificCatalogCategories.Context.SubmitChanges(); var otherCategory = from c in context.CatalogCategories where c.Name == "Others" select c; int otherCategoryID = 0; if (otherCategory.Count() == 0) { otherCategoryID = CreateCatalogCategories(new Category() { Name = "Others" }); } else { otherCategoryID = otherCategory.Single <CatalogCategory>().Id; } var machineries = from machinery in category.Machineries select machinery; foreach (var machinery in machineries) { machinery.Category = otherCategoryID; } var accesses = from acc in category.AccessCatalogCategories select acc; foreach (var acc in accesses) { context.AccessCatalogCategories.DeleteOnSubmit(acc); } context.AccessCatalogCategories.Context.SubmitChanges(); context.CatalogCategories.DeleteOnSubmit(category); context.CatalogCategories.Context.SubmitChanges(); } catch { return(false); } return(true); }
public bool UpdateCatalogCategories(Category categoryModel) { try { CatalogDatabaseDataContext context = new WorkLinqToSql.CatalogDatabaseDataContext(); var categoryNames = from cat in context.CatalogCategories select cat.Name; if (!categoryNames.Contains(categoryModel.Name)) { WorkLinqToSql.CatalogCategory category = (from c in context.CatalogCategories where c.Id == categoryModel.ID select c).Single <WorkLinqToSql.CatalogCategory>(); category.Name = categoryModel.Name; context.CatalogCategories.Context.SubmitChanges(); } } catch { return(false); } return(true); }