Is a linq query to ConcurrentDictionary Values threadsafe?

Correction… I’m not sure when you are accessing the Values property. It is thread safe when using LINQ on the object itself.


LINQ will use the GetEnumerator method to itterate the items.

Straight from MSDN

The enumerator returned from the dictionary is safe to use concurrently with reads and writes to the dictionary, however it does not represent a moment-in-time snapshot of the dictionary. The contents exposed through the enumerator may contain modifications made to the dictionary after GetEnumerator was called

if myDict.Any(x => !x.Value.HasPaid))
{
  return false
}

Leave a Comment