I want to store multiple values in single key like:
HashTable obj = new HashTable();
obj.Add(\"1\", \"test\");
obj.Add(\"1\", \"Test1\");
Right
Store a list in the hashtable:
obj.Add("1",new List<string>());
(obj["1"] as List<string>).Add("test");
(obj["1"] as List<string>).Add("test1");
This is a common trick.
Probably it is 4 years later, but I hope it will help somebody later. As mentioned earlier in the post, it is not possible to use the same key for different values in Hashtable(key, value). Although, you may create a List or some object as a value in the key/value pair of HashTable.
//instantiate new Hashtable
Hashtable hashtable = new Hashtable();
//create a class that would represent a value in the HashTable
public class SomeObject
{
public string value1 { get; set;}
public string value2 { get; set;}
}
//create a List that would store our objects
List<SomeObject> list = new List<someObject>();
//add new items to the created list
list.Add(new SomeObject()
{
value1 = "test",
value2 = "test1"
});
list.Add(new SomeObject()
{
value1 = "secondObject_value1"
value2 = "secondObject_value2"
})
//add key/value pairs to the Hashtable.
hashTable.Add("1", list[0]);
hashTable.Add("2", list[1]);
Then to retrieve this data:
//retrieve the value for the key "1"
SomeObject firstObj = (SomeObject)hashTable[1];
//retrieve the value for the key "2"
SomeObject secondObj = (SomeObject)hashTable[2];
Console.WriteLine("Values of the first object are: {0} and {1}",
firstObj.value1,firstObj.value2);
Console.WriteLine("Values of the second object are {0} and {1}",
secondObj.value1, secondObj.value2);
// output for the WriteLine:
Values of the first object are: test and test1
Values of the second object are secondObject_value1 and secondObject_value2
I'm using my own MultiDictionary
class. It's based on a Dictionary<TKey,List<TValue>>
but offers a bit of syntax sugar on top of that. Should be easy to extent Entry<TValue>
to implement IList<T>
public class MultiDictionary<TKey, TValue>
{
private Dictionary<TKey, List<TValue>> data = new Dictionary<TKey, List<TValue>>();
public struct Entry : IEnumerable<TValue>
{
private readonly MultiDictionary<TKey, TValue> mDictionary;
private readonly TKey mKey;
public TKey Key { get { return mKey; } }
public bool IsEmpty
{
get
{
return !mDictionary.data.ContainsKey(Key);
}
}
public void Add(TValue value)
{
List<TValue> list;
if (!mDictionary.data.TryGetValue(Key, out list))
list = new List<TValue>();
list.Add(value);
mDictionary.data[Key] = list;
}
public bool Remove(TValue value)
{
List<TValue> list;
if (!mDictionary.data.TryGetValue(Key, out list))
return false;
bool result = list.Remove(value);
if (list.Count == 0)
mDictionary.data.Remove(Key);
return result;
}
public void Clear()
{
mDictionary.data.Remove(Key);
}
internal Entry(MultiDictionary<TKey, TValue> dictionary, TKey key)
{
mDictionary = dictionary;
mKey = key;
}
public IEnumerator<TValue> GetEnumerator()
{
List<TValue> list;
if (!mDictionary.data.TryGetValue(Key, out list))
return Enumerable.Empty<TValue>().GetEnumerator();
else
return list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public Entry this[TKey key]
{
get
{
return new Entry(this, key);
}
}
}
You can use NameValueCollection - works the same as hashtable and has the "GetValues()".
you can put your test,test1,test2,...
in a table and then put this table in a Hashtable as a value for the key which will be the same for all them.
For example try something like this:
List<string> list = new List<string>();
list.Add("test");
list.Add("test1");
and then:
HashTable obj = new HashTable();
obj.Add("1", list);
You're looking for a Lookup, which can natively store multiple values for each key.
As pointed out this only works for a fixed list since you cannot add entries to a lookup once you have created it.
public class LookupEntry
{
public string Key { get; set; }
public string Value { get; set; }
}
var list = new List<LookupEntry>(new LookupEntry []
{
new LookupEntry() {Key="1", Value="Car" },
new LookupEntry() {Key="1", Value="Truck"},
new LookupEntry() {Key="2", Value="Duck"}
});
var lookup = list.ToLookup(x => x.Key, x => x.Value);
var all1s = lookup["1"].ToList();