How to get the key to a value in a hash table

I have a hashtable with a key and values, and in my code I repeat the hash table through the values, as shown below:

foreach (Object clientObject in ClientList.Values)
{
    // code to perform operation based on value
    ......
}

Where is the ClientList hashtable. Now I want to get the key for perticualar value from hashtable in my code. is there any way to achieve this?

thank

+5
source share
3 answers

You need to iterate through the table as follows:

        Hashtable clientList = new Hashtable();

        foreach (DictionaryEntry dictionaryEntry in clientList)
        {
            // work with value.
            Debug.Print(dictionaryEntry.Value.ToString());

            // work with key.
            Debug.Print(dictionaryEntry.Key.ToString());
        }
+5
source

If you want the list to keyshave a specific value using LINQ (.NET> = 3.5)

object searchedValue = something;

IEnumerable<object> keys = ClientList.Cast<DictionaryEntry>().Where(p => ClientList.Values == searchedValue).Select(p => p.Key);

This is probably not what you wanted, but it is what you requested.

( : ClientList ( "" ) , , . .

+3

Unable to get key using value.

One reason is that the key is unique, and the value is not unique in HashTable.

You can use the @Fischermaen method to read the value.

+1
source

All Articles