编辑:我误读了您的问题,实际上是关于属性更新,而不是集合更新。因此,如果您确实想更新Price
集合中所有项目的属性,那么Where
下面示例的子句当然不会帮助您。
你实际上并没有修改你的收藏;)
stockCollection.ToList().ForEach((s) => s.Price = DateTime.Now.Millisecond);
你可能想做:
stockCollection = new ConcurrentBag(stockCollection.Where(...));
编辑:
意思是,我每次都需要创建一个新的集合对象?
由于您的收藏没有实现INotifyCollectionChanged
nor INotifyPropertyChanged
,是的。如果可能,我建议使用ObservableCollection
而不是您当前的集合类型。ObservableCollection
能够通知项目属性的更新,以及在添加/删除项目时引发事件。
ObservableCollection<YourType> myCollection = new ObservableCollection<YourType>();
...
public ObservableCollection<YourType> MyCollection
{
get
{
return this.myCollection;
}
set
{
if (value != this.myCollection)
{
this.myCollection = value;
this.RaisePropertyChanged("MyCollection");
}
}
}
...
// Following lines of code will update the UI because of INotifyCollectionChanged implementation
this.MyCollection.Remove(...)
this.MyCollection.Add(...)
// Following line of code also updates the UI cause of RaisePropertyChanged
this.MyCollection = new ObservableCollection<YourType>(this.MyCollection.Where(z => z.Price == 45));