C# 如何填充词典中的词典?
我正在尝试填充一本又一本的字典。然而,当我尝试填充我的第三个字典时,我得到以下错误。如何填充第二个字典而不出错C# 如何填充词典中的词典?,c#,list,dictionary,nested,C#,List,Dictionary,Nested,我正在尝试填充一本又一本的字典。然而,当我尝试填充我的第三个字典时,我得到以下错误。如何填充第二个字典而不出错 The best overloaded method match for 'System.Collections.Generic.Dictionary<string,System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>>>.this
The best overloaded method match for 'System.Collections.Generic.Dictionary<string,System.Collections.Generic.Dictionary<string,
System.Collections.Generic.List<string>>>.this[string]' has some invalid arguments
//code
ClientsData.Add(new MapModel.ClientInfo { Id = IDCounter, Doctors = new Dictionary<string, Dictionary<string,List<string>>>() });
ClientsData[0].Doctors.Add(Reader["DocID"].ToString(), new Dictionary<string,List<string>>());
ClientsData[0].Doctors[0].Add("Name", new List<string>(){ Reader["DocName"].ToString()});//Error occurs here
与“System.Collections.Generic.Dictionary.this[string]”匹配的最佳重载方法具有一些无效参数
//代码
Add(new-MapModel.ClientInfo{Id=IDCounter,Doctors=new Dictionary()});
ClientsData[0].Doctors.Add(Reader[“DocID”].ToString(),new Dictionary());
ClientsData[0]。医生[0]。添加(“名称”,新列表(){Reader[“DocName]”。ToString()})//这里发生错误
要访问这样的词典,您需要使用一个键,在您的情况下,该键是一个字符串:
ClientsData[0].Doctors[Reader["DocID"].ToString()].Add("Name", new List<string>(){ Reader["DocName"].ToString()});
ClientsData[0]。医生[Reader[“DocID”].ToString()]。添加(“Name”,new List(){Reader[“DocName”].ToString());
如果您想使用三重录音,可以使用以下片段:
var dict = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
dict["level-one"] = new Dictionary<string, Dictionary<string, string>>();
dict["level-one"]["level-two"] = new Dictionary<string, string>();
dict["level-one"]["level-two"]["level-three"] = "hello";
Console.WriteLine(dict["level-one"]["level-two"]["level-three"]);
var dict=newdictionary();
dict[“一级”]=新字典();
dict[“一级”][“二级”]=新字典();
口述[“一级”][“二级”][“三级”]=“你好”;
控制台写入线(dict[“一级”][“二级”][“三级”];
或者,您可以像这样制作自己的包装:
public class TrippleDictionary<TKey, TValue>
{
Dictionary<TKey, Dictionary<TKey, Dictionary<TKey, TValue>>> dict = new Dictionary<TKey, Dictionary<TKey, Dictionary<TKey, TValue>>>();
public TValue this [TKey key1, TKey key2, TKey key3]
{
get
{
CheckKeys(key1, key2, key3);
return dict[key1][key2][key3];
}
set
{
CheckKeys(key1, key2, key3);
dict[key1][key2][key3] = value;
}
}
void CheckKeys(TKey key1, TKey key2, TKey key3)
{
if (!dict.ContainsKey(key1))
dict[key1] = new Dictionary<TKey, Dictionary<TKey, TValue>>();
if (!dict[key1].ContainsKey(key2))
dict[key1][key2] = new Dictionary<TKey, TValue>();
if (!dict[key1][key2].ContainsKey(key3))
dict[key1][key2][key3] = default(TValue);
}
}
var tripple = new TrippleDictionary<string, string>();
tripple["1", "2", "3"] = "Hello!";
Console.WriteLine(tripple["1", "2", "3"]);
公共类TrippleDictionary
{
Dictionary dict=新字典();
公共TValue此[TKey-key1,TKey-key2,TKey-key3]
{
得到
{
检查键(键1、键2、键3);
返回dict[key1][key2][key3];
}
设置
{
检查键(键1、键2、键3);
dict[key1][key2][key3]=值;
}
}
无效检查键(TKey key1、TKey key2、TKey key3)
{
如果(!dict.ContainsKey(键1))
dict[key1]=新字典();
如果(!dict[key1].ContainsKey(key2))
dict[key1][key2]=新字典();
如果(!dict[key1][key2].ContainsKey(key3))
dict[key1][key2][key3]=默认值(TValue);
}
}
然后像这样使用它:
public class TrippleDictionary<TKey, TValue>
{
Dictionary<TKey, Dictionary<TKey, Dictionary<TKey, TValue>>> dict = new Dictionary<TKey, Dictionary<TKey, Dictionary<TKey, TValue>>>();
public TValue this [TKey key1, TKey key2, TKey key3]
{
get
{
CheckKeys(key1, key2, key3);
return dict[key1][key2][key3];
}
set
{
CheckKeys(key1, key2, key3);
dict[key1][key2][key3] = value;
}
}
void CheckKeys(TKey key1, TKey key2, TKey key3)
{
if (!dict.ContainsKey(key1))
dict[key1] = new Dictionary<TKey, Dictionary<TKey, TValue>>();
if (!dict[key1].ContainsKey(key2))
dict[key1][key2] = new Dictionary<TKey, TValue>();
if (!dict[key1][key2].ContainsKey(key3))
dict[key1][key2][key3] = default(TValue);
}
}
var tripple = new TrippleDictionary<string, string>();
tripple["1", "2", "3"] = "Hello!";
Console.WriteLine(tripple["1", "2", "3"]);
var tripple=new TrippleDictionary();
三个字母[“1”、“2”、“3”]=“你好!”;
控制台写入线(三个[“1”、“2”、“3”);
看我要把这个扔出去:这是一种巨大的代码气味。我想说的是大的设计问题。你最好制作一个定制的
struct ComplexKey
,它具有必要的等式/哈希代码魔法,可以充当一个字典的键。也就是说,我同意@Simon的评论,你可能想在这里重新思考你的设计…ContainsKey
是间接的。使用TryGetValue
和Add
合并中的值。