Reputation: 6027
Is there a way without looping through the entire hashtable to remove key-value pairs with a specified value?
If not- is there a better type that I can use which will allow me to do this?
Here's a bit of background as to what I'm doing. I'm sure I'm overcomplicating and there's a better approach...
I have a list of key/values where the key is a column name in a "source" spreadsheet and the value is a field name in a "destination" system.
As I loop through the columns in the "source" and match the keys in the hashtable I can remove the pairs.
Once I am finished there may be columns in the "source" with no matching key in the hashtable. The user can override this by picking one of the remaining values.
This is where I then need to remove the that key/value from the hashtable.
Upvotes: 0
Views: 834
Reputation: 203817
No, there is not.
If you want to do this it usually means one of two things
You have your keys and values reversed. Make your values your keys and your keys your values.
You need two dictionaries. One Dictionary<T1, T2>
and one Dictionary<T2,T1>
. You'll need to populate it such that each pair is in both dictionaries and is consistent. it also means that each value much be unique, rather than just the keys. If your current values aren't unique, then you'd need a Dictionary<T2, List<T1>>
or something similar, since each value might map to multiple keys. Doing that would also make maintaining your structures...harder. (Consider making a new TwoWayDictionary
type if that's what you need.)
Upvotes: 5
Reputation: 111
The HashTable Class implements IEnumerable so you can use query (LINQ) instead of Iterating
Upvotes: -1
Reputation: 1500385
Is there a way without looping through the entire hashtable to remove key-value pairs with a specified value?
No. The point of a hashtable is that it's quick to look up by key, not value.
Note that you should generally be using Dictionary<TKey, TValue>
anyway, unless you're really using .NET 1.1.
If not- is there a better type that I can use which will allow me to do this?
If you're going to have unique values as well as unique keys, you could just have two dictionaries, one each way. Then you can look up the key which corresponds to the relevant value, and remove from both dictionaries by key.
Upvotes: 5