I have a ConcurrentDictionary. I use its AddOrUpdate method to manipulate its items.
My question is: is it possible to use AddOrUpdate‘s update parameter to contain an if statement? E.g. my ConcurrentDictionary contains objects that has string Id and a DateTime Date properties.
I’d like to
– add a new object to it, if the object with the given Id does not exist
– update it, if the new object’s Date is equal or greater, than the existing one, if it is less, than does not do anything.
In my example:
Dictionary.AddOrUpdate(testObject.Id,testObject,(k, v) => v);
I should change
(k, v) => v
to
if(v.Date >= existingItem.Date) (k, v) => v
else do nothing
vis the value that currently exists in the collection, so to do nothing just return it.More readable: