public async Task MapbrochureForCategory(string FromBrochureID, string ToBrochureID, string Categories, string Option, string CopytoCategory) { int bID, TBID, cID, toCID; int.TryParse(FromBrochureID, out bID); int.TryParse(ToBrochureID, out TBID); int.TryParse(CopytoCategory, out toCID); // List<Product> plist = getProductsByBrochureID_and_CategoryID(BrochureID, CategoryID); await Task.Run(() => { // List<Category> categoryList = GetByBrochureID(FromBrochureID).Where(x => x.check == "checked").ToList(); if (Option == "2") { if (!string.IsNullOrEmpty(Categories)) { //seprate product ids var cats = Categories.Split(','); MapCategory mp = null; List <MapCategory> mplist = null; foreach (string c in cats) { cID = int.Parse(c); mplist = db.mappings.Where(x => x.BrochureID == bID && x.CategoryID == cID).ToList(); mplist.ForEach(x => { mp = new MapCategory(); mp.BrochureID = TBID; mp.CategoryID = toCID; mp.ProductID = x.ProductID; db.mappings.Add(mp); db.SaveChanges(); }); } } } else if (Option == "1") { if (!string.IsNullOrEmpty(Categories)) { //seprate product ids var cats = Categories.Split(','); MapCategory mp = null; List <MapCategory> mplist = null; foreach (string c in cats) { cID = int.Parse(c); mplist = db.mappings.Where(x => x.BrochureID == TBID && x.CategoryID == toCID).ToList(); mplist.ForEach(x => { db.mappings.Remove(x); db.SaveChanges(); }); mplist = db.mappings.Where(x => x.BrochureID == bID && x.CategoryID == cID).ToList(); mplist.ForEach(x => { mp = new MapCategory(); mp.BrochureID = TBID; mp.CategoryID = toCID; mp.ProductID = x.ProductID; db.mappings.Add(mp); db.SaveChanges(); }); } } } }); }
public async Task Mapbrochure(string BrochureID, string CategoryID, string Products) { int bID, cID, pID; int.TryParse(BrochureID, out bID); int.TryParse(CategoryID, out cID); List <Product> plist = getProductsByBrochureID_and_CategoryID(BrochureID, CategoryID); if (!string.IsNullOrEmpty(Products)) { await Task.Run(() => { //seprate product ids var products = Products.Split(','); //take out products which are in products array List <Product> list = plist.Where(c => products.Contains(c.ID.ToString())).ToList(); //take out products which are not in products array List <Product> templist = plist.Where(c => !products.Contains(c.ID.ToString())).ToList(); //Products to be deleted List <Product> deleteList = templist.Where(x => x.check == "checked").ToList(); //mapped products List <Product> mappedList = plist.Where(x => x.check == "checked").ToList(); //products to update in database List <Product> updatelist = list.Except(mappedList).ToList(); //add Records in mapping if (updatelist.Count > 0) { MapCategory mp = new MapCategory(); foreach (Product p in updatelist) { mp.BrochureID = bID; mp.CategoryID = cID; mp.ProductID = p.ID; db.mappings.Add(mp); db.SaveChanges(); } //updatelist.ForEach(m => db.SaveChanges()); } //delete Records from mapping if (deleteList.Count > 0) { MapCategory mp = null; foreach (Product p in deleteList) { mp = db.mappings.Single(x => x.BrochureID == bID && x.CategoryID == cID && x.ProductID == p.ID); db.mappings.Remove(mp); db.SaveChanges(); } } }); } else { await Task.Run(() => { var products = plist.Where(x => x.check == "checked").ToList(); products.ForEach(x => { db.Products.Remove(x); }); db.SaveChanges(); }); } }