Change my new list without changing the original list

By | July 19, 2018

This is my original list:

var cart = _workContext.CurrentCustomer.ShoppingCartItems
                        .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)

I reviewed this link : How do I change my new list without changing the original list?

And have understood that I have a method for copy so this is my method:

private List<ShoppingCartItem> CopyShoppingCartItems(List<ShoppingCartItem> target)
    //var tmp = new List<ShoppingCartItem>(target);
    var tmp = target.ToList();
    return tmp;

I test var var tmp = new List<shoppingCartItem>(target)too.
this is my goal:

var beforeUpdatCart = CopyShoppingCartItems(cart);
foreach (var sci in cart)
    var remove = allIdsToRemove.Contains(sci.Id);
    if (remove)
        _shoppingCartService.DeleteShoppingCartItem(sci, ensureOnlyActiveCheckoutAttributes: true);
        foreach (var formKey in form.Keys)
            if (formKey.Equals($"itemquantity{sci.Id}", StringComparison.InvariantCultureIgnoreCase))
                int newQuantity;
                if (int.TryParse(form[formKey], out newQuantity))
                    sci.Quantity = newQuantity;

but newQuantity is inserted in both List<shoppingCartItem> : cart and beforeUpdateCart.

Can any one help me?


Leave a Reply

Your email address will not be published. Required fields are marked *