public void SaveChars(DataContext DB, List <ProductModelChar> chars) { var exist = new List <ProductChar>(); foreach (var ch in chars) { var chd = ProductChars.FirstOrDefault(x => String.Equals(x.Name, ch.Name, StringComparison.CurrentCultureIgnoreCase)); if (chd != null) { exist.Add(chd); chd.Value = ch.Value; } else { var c = new ProductChar() { Name = ch.Name, Value = ch.Value, OrderedProductID = ID }; exist.Add(c); DB.ProductChars.InsertOnSubmit(c); } } DB.SubmitChanges(); var foreDel = ProductChars.Where(x => exist.All(z => z.ID != x.ID)); DB.ProductChars.DeleteAllOnSubmit(foreDel); DB.SubmitChanges(); }
public ActionResult Details(int Id) { ProductDetailsModel productDetailsModel = new ProductDetailsModel(); productDetailsModel.ProductCharsList = new List <ProductCharModel>(); productDetailsModel.Parent = this.dc.Products.Where <Product>((Expression <Func <Product, bool> >)(p => p.Id == Id)).FirstOrDefault <Product>(); ProductPrice productPrice = this.dc.ProductPrice.Where <ProductPrice>((Expression <Func <ProductPrice, bool> >)(pr => pr.ProductId == Id)).SingleOrDefault <ProductPrice>(); if (productPrice != null) { productDetailsModel.Price = productPrice.Price; } IQueryable <ProductChars> source1 = this.dc.ProductChars.Where <ProductChars>((Expression <Func <ProductChars, bool> >)(pch => pch.ProductId == Id)); if (source1.Any <ProductChars>()) { foreach (ProductChars productChars in (IEnumerable <ProductChars>)source1) { ProductChars ch = productChars; ProductCharModel productCharModel = new ProductCharModel(); productCharModel.StockList = new List <StockModel>(); productCharModel.Characteristic = ch; IQueryable <ProductCharRemains> source2 = new DataContext().ProductCharRemains.Where <ProductCharRemains>((Expression <Func <ProductCharRemains, bool> >)(pcr => pcr.ProductId == ch.ProductId && pcr.ProductCharId == ch.Id)); if (source2.Any <ProductCharRemains>()) { productCharModel.TotalRemains = source2.Sum <ProductCharRemains>((Expression <Func <ProductCharRemains, double> >)(x => x.Remains)); foreach (ProductCharRemains productCharRemains in (IEnumerable <ProductCharRemains>)source2) { ProductCharRemains shk = productCharRemains; StockModel stockModel1 = productCharModel.StockList.Find((Predicate <StockModel>)(x => x.CStock.Id == shk.StockId)); if (stockModel1 == null) { StockModel stockModel2 = new StockModel(); stockModel2.ShkList = new List <ShkModel>(); DataContext dataContext = new DataContext(); stockModel2.CStock = dataContext.Stocks.Where <Stock>((Expression <Func <Stock, bool> >)(s => s.Id == shk.StockId)).FirstOrDefault <Stock>(); stockModel2.ShkList.Add(new ShkModel() { Shk = shk.Shk, Remains = shk.Remains }); productCharModel.StockList.Add(stockModel2); } else { stockModel1.ShkList.Add(new ShkModel() { Shk = shk.Shk, Remains = shk.Remains }); } } } productDetailsModel.ProductCharsList.Add(productCharModel); } } return((ActionResult)this.View((object)productDetailsModel)); }