Effective way to get a series of keys from a dictionary

I have Dictionarythat for most operations I just need to get one record by key, but for several operations I will need to work with records associated with a number of keys. The way that arises for me is to use GetKeysand FindAll, which will correspond to the range of interest to me, but wondered if anyone could suggest a better method.

+3
source share
3 answers

A Dictionary, which is implemented as a hash table, is not particularly suitable for efficiently performing range selection operations on keys. You will need to find all the keys in order to find all of them in the specified range. A good way to achieve this is to query its key collection with a simple LINQ expression.

+4
source

A SortedListor SortedDictionarywill be sorted so that you can get the key at the bottom of the range, and then cross the elements at the top of the range.

Using binary search by SortedListwill give you the index of the key corresponding to the bottom of your range, or the nearest higher value. See How to perform binary search on IList <T>?

+3
source

, . , - :

dictionary.FindAll(entry => multipleStrings.Contains(entry.Key));
+1

Source: https://habr.com/ru/post/1714786/


All Articles