H#中的Hastable:多次使用相同的密钥

use*_*378 4 c# hashtable

我获取具有相同ID的多个记录,我想将它们存储Hashtable在C#中.我使用id作为键中的键,Hashtable值是对象本身.它会引发异常,因为会再次添加相同的密钥.有没有办法解决这个问题?

这是我的代码:

Hashtable localItemsIndex = new Hashtable();            
foreach (TzThing thing in localThingz)
         localItemsIndex.Add(thing.Id, thing); 
Run Code Online (Sandbox Code Playgroud)

在此先感谢jennie

Ste*_*cya 7

也许您应该使用Dictionary<Id,List<TzThing>>为一个键存储多个值

public void Add(YourIdType key,TzThing thing )
{
   if(dictionary.ContainsKey(key))
   {
      dictionary[key].Add(thing);
   }
   else
   {
      dictionary.Add(key,new List<TzThing> {thing});
   }
}
Run Code Online (Sandbox Code Playgroud)