识别和替换 ObservableCollection 中的对象的最有效方法是什么?
我有一个方法接收已更改属性的客户对象,我想通过替换该对象的旧版本将其保存回主数据存储中。
有谁知道正确的 C# 方法来编写下面的伪代码来执行此操作?
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有效是避免 LINQ ;-p
如果您想使用 LINQ:这并不理想,但至少可以工作:
它通过 ID 找到它们(使用 LINQ),然后使用 < code>IndexOf 获取位置,索引器更新它。 风险更大一点,但只需扫描一次:
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
IndexOf
to get the position, and the indexer to update it. A bit more risky, but only one scan: