I have a method that receives a customer object which has changed properties and I want to save it back into the main data store by replacing the old version of that object.
Does anyone know the correct C# way to write the pseudo code to do this below?
public static void Save(Customer customer)
{
ObservableCollection<Customer> customers = Customer.GetAll();
//pseudo code:
var newCustomers = from c in customers
where c.Id = customer.Id
Replace(customer);
}
The most efficient would be to avoid LINQ ;-p
If you want to use LINQ: this isn’t ideal, but would work at least:
It finds them by ID (using LINQ), then uses
IndexOfto get the position, and the indexer to update it. A bit more risky, but only one scan: