編集:実際にはコレクションの更新ではなく、プロパティの更新に関するあなたの質問を読み違えました。したがってPrice
、コレクションのすべてのアイテムのプロパティを実際に更新したい場合Where
は、もちろん、以下の例の節は役に立ちません。
あなたは実際にあなたのコレクションを変更していません;)
stockCollection.ToList().ForEach((s) => s.Price = DateTime.Now.Millisecond);
あなたがしたいかもしれません:
stockCollection = new ConcurrentBag(stockCollection.Where(...));
編集:
つまり、毎回新しいコレクション オブジェクトを作成する必要があるということですか?
コレクションが実装されていないのでINotifyCollectionChanged
、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));