Q:

How can i Add or Update this .NET collection, during a Parallel. ForEach? 

I have a list of files, where each file contains a list of Foo data.  Now, the same piece of Foo data (eg.  Id = 1) might exist in multiple files, but the more recent piece of data would overwrite an existing one. 
I'm just reading each piece of data into an in memory collection. 
if ! cache. HasKey(foo. Id) then Add    
else cache[foo. Id].UpdatedOn < foo. UpdatedOn then Update  
else do nothing

When i'm reading in the files (cause there's a few of em), I'm also using Parallel. ForEach(files, file => { .. });
I'm not sure how I do this. 
I was thinking of using a ConcurrentDictionary but I wasn't sure how to do an AddOrUpdate with where clause thingy. 
Any suggestions? 

A:

You can use a ConcurrentDictionary, like so:
dictionary. AddOrUpdate(foo. Id, foo, (id, existing) => 
    existing. UpdatedOn < foo. UpdatedOn ?  foo : existing);

Due to the discussion in the comments below, I will explain why there's no race condition here.  This MSDN article discusses how value factories are run, and mentions that:

Therefore, it is not guaranteed that the data that is returned by GetOrAdd is the same data that was created by the thread's valueFactory. 

This makes sense, since the designers of the concurrent dictionary didn't want user code to lock the dictionary for who knows how long, rendering it useless.  Instead, what AddOrUpdate does is run in two nested loops.  Here's some pseudo-code:
do { 
   while (! TryGetValue(key, out value))
       if (TryAdd(key, addValue)) return;
   newValue = updateValueFactory(key, value);
} while (TryUpdate(key, newValue, value));

TryUpdate acquires the lock for the specific bucket, compares the current value to the retrieved value, and only if they match performs the update.  If this fails, the outer loop happens again, TryGetValue returns the latest value, the value factory is called again, and so forth. 
So it is assured that the value factory will always have the latest value if the update succeeds.