Let's say that I have a ConcurrentDictionary:
var dict = new ConcurrentDictionary<string, someObject>();
dict.TryAdd("0_someA_someB_someC", obj0);
dict.TryAdd("1_someA_someB_someC", obj1);
dict.TryAdd("2_someA_someB_someC", obj2);
dict.TryAdd("3_someA_someB_someC", obj3);
The <number>_ in the keys is incremented and being a dictionary, there is no guarantee that the elements are in order.
Now, imagine I wanted to remove all items from the dictionary that have the number less than 2. I have no idea what the keys will look like, only that they will be prefixed with a number as above.
How can I remove all elements from the dictionary who's key starts with a value less than 2?
For example, the resulting dict after this process will look like this:
dict.TryAdd("2_someA_someB_someC", obj2);
dict.TryAdd("3_someA_someB_someC", obj3);
ConcurrentDictionary(but not necessarily, depending on why you're usingConcurrentDictionaryand why you're removing keys).ImmutableSortedDictionaryis a thing, but may or may not be a good match for your scenario -- again, depending on use.ValueTupleis easy enough in recent versions of C#:var d = new ConcurrentDictionary<(int theNumber, string compoundKey), someObject>(); d.TryAdd((2, "2_someA_someB_someC"), obj0); d.Keys.Where(k => k.theNumber < 2).ValueTuplehas suitable implementations ofGetHashCodeandEqualsto work as a dictionary key. This only optimizes the string parsing and not the fact that we still have to go through all the keys, but that may be enough. The custom adapter that splits dictionaries is a bunch more work that I don't feel like working on. :-P