C# 如何更新集合中的元素而不是引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2049381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:00:12  来源:igfitidea点击:

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

c#

提问by GuestMVCAsync

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)

我有一个 ProductSearchResults 集合,下面的方法旨在在该集合中查找特定产品并更新它。我最终更新了指向集合元素的对象而不是它自己的实际元素(我认为)

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;
        }
    }

采纳答案by Jamiec

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

实际上,您所做的只是更改对局部变量的引用toUpdate以指向传入的参数product

Lets take a step backwards, when you do:

当你这样做时,让我们向后退一步:

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

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

您有集合中的项目的引用( ProductSearchResults)。你现在可以愉快地更新它的属性了,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 itme from the collection, and add your new one if that is indeed what you require:

但是,您无法按照您尝试的方式更新集合中的 itemn 以指向不同的/新项目。您可以从集合中删除该项目,如果确实需要,则添加新项目:

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.

希望有帮助。

回答by David

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

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

回答by Victor Nicollet

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.

在 C# 中,写入variable = expression;会为变量分配一个新值。它不会影响该变量先前引用的值。

In your example, you have a value called product, which is a ProductInfoprovided 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 toUpdatebased on values in product.

在您的示例中,您有一个名为 的值product,它是ProductInfo由调用者提供的,以及您从存储在 中的列表中获取的值toUpdate,您希望更新该值。这将涉及toUpdate基于 中的值调用成员函数(或分配给属性)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 ProductInfoassociated with a certain identifier, so all you need to do is call that function.

但是,我怀疑您实际上想要更新在数据库或由ProductSearchResults. 您使用的存储引擎(或您的数据访问层)可能为您提供了一个函数来保存ProductInfo与某个标识符相关联的 a,因此您需要做的就是调用该函数。

回答by Jamie Ide

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
    }
}