Avatar of Dinesh Bali
Dinesh Bali

asked on 

Update child object reference

Hi,

I have object called productbzAux  and it product type has value "A". I wanted to duplicate the same product with productType as "C" and add it to the same List object productCategoryBEList

My below code is updating the productType for parent and child object i.e., productbzAux  and productbzCore

Also, please advise the best way to write the below code.

The below code is in C#

Thanks

List<ProductCategoryBE> productCategoryBEList = shoppingCartBE.ArrivalBooking.AvailableProducts;

            if (productCategoryBEList != null)
            {
                ProductCategoryBE productCategoryAUXBE = productCategoryBEList.FirstOrDefault(p => p.CategoryCode.ToUpper().Contains("AUX"));
                ProductItem productbzAux = productCategoryAUXBE.Products.FirstOrDefault(t => t.SubCategoryCode.ToUpper().Contains("bz"));

                if (productbzAux != null)
                {
                    productCategoryBEList.Remove(productCategoryAUXBE);
                    List<ProductItem> products = productCategoryAUXBE.Products;
                    ProductItem productbzCore = new ProductItem();
                    productbzCore = productbzAux;
                    productbzCore.ProductType = "C"; // This is updating the ProductType of parent object i.e., productbzAux.ProductType where as it should be A for parent and C for child.
                    products.Add(productbzCore);

                    productCategoryAUXBE.Products = products;

                    productCategoryBEList.Add(productCategoryAUXBE);
                }
            }

Open in new window


Regards,
C#

Avatar of undefined
Last Comment
Dinesh Bali

8/22/2022 - Mon