public static ProductOptionViewModel GetProductById(int id) { ProductOptionViewModel productAtrributeViewModel = new ProductOptionViewModel(); List<ProductAtrributeOptionsViewModel> viewModel = new List<ProductAtrributeOptionsViewModel>(); using (EcommerceEntities entities = new EcommerceEntities()) { //ResponseViewModel<ProductAtrributeOptionsViewModel> response = new ResponseViewModel<ProductAtrributeOptionsViewModel>(); List<Product_Attribute> options = new List<Product_Attribute>(); List<ProductAttribute> attributes = new List<ProductAttribute>(); //List<ProductAttributeOption> options = new List<ProductAttributeOption>(); GetProductById_Result result = entities.GetProductById(id).FirstOrDefault();//get api of product Product product = new Product(); product.Name = result.Name; product.ID = result.ID; product.Price = result.Price; product.ProductCateogoryID = result.CategoryId; product.ProductSubCategoryID = result.SubCategoryID; product.Image = result.Image; product.Description = result.Description; product.Discount = result.Discount; product.Quentity = result.Quentity; options = entities.Product_Attribute.Where(entry => entry.ProductID == id).ToList(); //attributes = entities.ProductAttributes.Where(entry => entry. == product.ProductSubCategoryID).ToList(); if (options != null) { foreach (var option in options) { ProductAtrributeOptionsViewModel attributeOptionViewModel = new ProductAtrributeOptionsViewModel(); ProductAtrributeOptionsViewModel attributeOptionViewModelObj = new ProductAtrributeOptionsViewModel(); List<OptionList> optionsList = new List<OptionList>(); ProductAttributeOption optionDb = new ProductAttributeOption(); ProductAttribute productAttributeDb = new ProductAttribute(); optionDb = entities.ProductAttributeOptions.Where(entry => entry.ID == option.OptionID).FirstOrDefault(); productAttributeDb = entities.ProductAttributes.Where(entry => entry.ID == optionDb.ProductAtrributeID).FirstOrDefault(); attributeOptionViewModel.attributeName = productAttributeDb.Name; attributeOptionViewModel.attributeId = Convert.ToInt32(productAttributeDb.ID); if(viewModel.Any(entry => entry.attributeId == attributeOptionViewModel.attributeId)) { attributeOptionViewModelObj = viewModel.Where(entry => entry.attributeId == attributeOptionViewModel.attributeId).FirstOrDefault(); if (attributeOptionViewModelObj != null) { OptionList optionViewModel = new OptionList(); optionViewModel.optionName = optionDb.Value; optionViewModel.optionId = Convert.ToInt32(optionDb.ID); attributeOptionViewModel.optionList = optionsList; attributeOptionViewModelObj.optionList.Add(optionViewModel); } } else { OptionList optionViewModel = new OptionList(); optionViewModel.optionName = optionDb.Value; optionViewModel.optionId = Convert.ToInt32(optionDb.ID); optionsList.Add(optionViewModel); attributeOptionViewModel.optionList = optionsList; viewModel.Add(attributeOptionViewModel); } } } productAtrributeViewModel.product = product; productAtrributeViewModel.ViewModel = viewModel; } return productAtrributeViewModel; }