I have a Dictionary<string, List<int>> in my code which I am using in the following manner:
Key Values
2011-07-15 1, 2, 3
2011-07-20 4, 5, 6
2010-02-11 7, 8, 9
My code needs to be able to query for all values matching a particular substring in the key. For example, if I had the substring 2011-07 it should return values {1, 2, 3, 4, 5, 6}. A substring of 11 should return all IDs from 1-9.
Can anyone recommend a concise way to achieve this? Or provide a better data structure for retrieving this information?
I would do an extension method :
The “cost” of adding values to the dictionary wouldn’t change, but the cost of retrieval would be higher, but only when you know you’re going with a partial match.
Btw, I’m sure you could transform this in a single Lambda expression, but the concept remains the same.
Edit: In your example, this method would return 2 lists of values, but you can change it to merge the lists. Here is the extension method you could do :
Edit 2: Come to think of it, you could also make it more generic. With the next extension method, it would work on any dictionary, as long as you provide a
comparerthat check what you mean by “partial match” :