GuestMVCAsync
GuestMVCAsync

Reputation: 597

How can i update an element in collection instead of the reference

I have a collection ProductSearchResults, below method intends to find a specific product in that collection and update it. I end up updating the object that points to the element of the collection instead of the actual element it self though(i think)

Can you please show me how to do this properly so that I update the actual product in the collection

Thanks

public void UpdateProductInfo(ProductInfo product)
    {
        var productToUpdate = this.ProductSearchResults.Where(p => p.ID == product.ID);

        if (productUpdate.Count() > 0)
        {
            var toUpdate = productToUpdate.First<ProductInfo>();

            toUpdate = product;
        }
    }

Upvotes: 13

Views: 33018

Answers (4)

Jamiec
Jamiec

Reputation: 136074

IN actual fact all you are doing is changing the reference to the local variable toUpdate to point at the passed-in argument product.

Lets take a step backwards, when you do:

var toUpdate = productToUpdate.First<ProductInfo>();

you have a reference to an item from your collection (ProductSearchResults). You can now happily update its properties, ala:

toUpdate.ProductName = product.ProductName;
toUpdate.Price = product.Price;
//etc..

however, you cannot update the itemn in the collection to point to a different/new item in the way you were attempting to. You could remove that item from the collection, and add your new one if that is indeed what you require:

public void UpdateProductInfo(ProductInfo product)
    {
        var productToUpdate = this.ProductSearchResults.Where(p => p.ID == product.ID);

        if (productUpdate.Count() > 0)
        {
            var toUpdate = productToUpdate.First<ProductInfo>();

            this.ProductSearchResults.Remove(toUpdate);
            this.ProductSearchResults.Add(product);
        }
    }

Hope that helps.

Upvotes: 20

Jamie Ide
Jamie Ide

Reputation: 49251

Do you want to replace the product in the collection or update some properties on it? Assuming the latter, here's an example:

public void UpdateProductInfo(ProductInfo product)
{
    var productToUpdate = this.ProductSearchResults.FirstOrDefault(p => p.ID == product.ID).;
    if (productToUpdate == null)
    {
        // throw exception?
    }
    else
    {
        productToUpdate.Price = productInfo.Price; // for example
    }
}

Upvotes: 0

Victor Nicollet
Victor Nicollet

Reputation: 24577

In C#, writing variable = expression; assigns a new value to the variable. It does not affect the value that was previously referenced by that variable.

In your example, you have a value called product, which is a ProductInfo provided by the caller, and a value you get from your list, which you stored in toUpdate, that you wish to update. This would involve calling member functions (or assigning to properties) of toUpdate based on values in product.

However, I suspect you actually want to update the product information of a product found in a database or other kind of storage that is accessed by ProductSearchResults. The storage engine you use (or your data access layer) probably provides you with a function to save a ProductInfo associated with a certain identifier, so all you need to do is call that function.

Upvotes: 0

David
David

Reputation: 15350

var productToUpdate = this.ProductSearchResults.FirstOrDefault(p => p.ID == product.ID);

if (productUpdate != null)
{
    productUpdate.Property = product.Property;
    ...continue for other properties
}

Upvotes: 3

Related Questions