'How to iterate through Hashtable that has values as List?
I wanted to have multiple values for a single key in my hashtable, so I created key value pair as
Hashtable A = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
List<string> DataList = new List<string>();
DataList.Add(userName);
DataList.Add(firstName);
DataList.Add(lastName);
DataList.Add(Pwd);
if (!A.ContainsKey(ID))
{
A.Add(ID, DataList);
}
For example, my hashtable contains values as
Hashtable A -> Key : 1 , Value : {'A','B','1234','@red'}
Hashtable B -> Key : 2, Value : {'Emma','B','111','@blue'}
I have two hashtables and I want to compare the date using the key. If the key is present in both tables, then I wish to compare the values of that key in both tables. If there is any change, then I output it.
For example, if ID- 1001 is present in both A and B, then I check the list of values in both table for 1001. If for example, A has third value '1234' and B has third value as '111', then I output 111.
Its just a comparison of hashtable but I am stuck at iterating through the values.
foreach(DictionaryEntry details in A)
{
if(details.Key.Equals(Key of Hashtable B))
{
//I want to do something like this if both Hashtables have same key
string firstname = 'A';
string lastname = 'B';
string password ='1234' and so on...
// I just wish to store the values of first hashtable in some variables so that I can use them further.
}
}
I tried using foreach but it gave me error that
Foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
How do I parse through this list of values in Hashtable?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
