EDIT: I misread your question that is in fact about a property update, not a collection update.
So if you actually wanted to update the Price
property all of the items of your collection, then Where
clause of the example below won't help you of course.
You're actually not modifying your collection ;)
stockCollection.ToList().ForEach((s) => s.Price = DateTime.Now.Millisecond);
You might want to do:
stockCollection = new ConcurrentBag(stockCollection.Where(...));
EDIT:
Means, I need to create a new collection object everytime?
As your collection does not implement INotifyCollectionChanged
nor INotifyPropertyChanged
, yes.
I would suggest using ObservableCollection
instead of your current collection type if possible. ObservableCollection
is able to notify of the update of an item property, as well as raise an event when an item is added/deleted.
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));