C# 如何在字典中检查containsKey<;字符串、字典<;字符串、字典<;字符串,字符串>&燃气轮机&燃气轮机;
我想检查下面提到的字典是否包含特定的键C# 如何在字典中检查containsKey<;字符串、字典<;字符串、字典<;字符串,字符串>&燃气轮机&燃气轮机;,c#,.net,generics,collections,C#,.net,Generics,Collections,我想检查下面提到的字典是否包含特定的键 Dictionary<string, Dictionary<string, Dictionary<string, string>>> 字典 您需要用3个键检查,一个用于外部dict,一个用于内部dict Dictionary<string, Dictionary<string, Dictionary<string, string>>> dict= new Dictionary<
Dictionary<string, Dictionary<string, Dictionary<string, string>>>
字典
您需要用3个键检查,一个用于外部dict,一个用于内部dict
Dictionary<string, Dictionary<string, Dictionary<string, string>>> dict= new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
if (dict.ContainsKey(outerKey))
{
var innerDict = dict[outerKey];
if (innerDict.ContainsKey(innerKey))
{
var innerMost = innerDict[innerKey];
if (innerMost.ContainsKey(innerMostKey))
var item = innerMost[innerMostKey]; // This is the item of inner most dict
}
}
Dictionary dict=new Dictionary();
if(dict.ContainsKey(outerKey))
{
var innerDict=dict[outerKey];
if(innerDict.ContainsKey(innerKey))
{
var innerMost=innerDict[innerKey];
if(最内层的ContainsKey(最内层的ContainsKey))
var item=innerMost[innerMostKey];//这是最内层dict的项
}
}
您需要用3个键检查,一个用于外部dict,一个用于内部dict
Dictionary<string, Dictionary<string, Dictionary<string, string>>> dict= new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
if (dict.ContainsKey(outerKey))
{
var innerDict = dict[outerKey];
if (innerDict.ContainsKey(innerKey))
{
var innerMost = innerDict[innerKey];
if (innerMost.ContainsKey(innerMostKey))
var item = innerMost[innerMostKey]; // This is the item of inner most dict
}
}
Dictionary dict=new Dictionary();
if(dict.ContainsKey(outerKey))
{
var innerDict=dict[outerKey];
if(innerDict.ContainsKey(innerKey))
{
var innerMost=innerDict[innerKey];
if(最内层的ContainsKey(最内层的ContainsKey))
var item=innerMost[innerMostKey];//这是最内层dict的项
}
}
你必须一个接一个地检查,我想是这样的:
Dictionary<string, Dictionary<string, Dictionary<string, string>>> dictionary =
new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
if (dictionary.ContainsKey("someKey"))
{
var secondDictionary = dictionary["someKey"];
if (secondDictionary.ContainsKey("otherKey"))
{
var thirdDictionary = secondDictionary["otherKey"];
if (thirdDictionary.ContainsKey("thirdKey"))
{
var final = thirdDictionary["thirdKey"];
}
}
}
字典=
新字典();
if(dictionary.ContainsKey(“someKey”))
{
var secondDictionary=dictionary[“someKey”];
if(secondDictionary.ContainsKey(“otherKey”))
{
var thirdDictionary=secondDictionary[“otherKey”];
if(第三个字典的ContainsKey(“第三个”))
{
var final=第三个字典[“第三个”];
}
}
}
你必须一个接一个地检查,我想是这样的:
Dictionary<string, Dictionary<string, Dictionary<string, string>>> dictionary =
new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
if (dictionary.ContainsKey("someKey"))
{
var secondDictionary = dictionary["someKey"];
if (secondDictionary.ContainsKey("otherKey"))
{
var thirdDictionary = secondDictionary["otherKey"];
if (thirdDictionary.ContainsKey("thirdKey"))
{
var final = thirdDictionary["thirdKey"];
}
}
}
字典=
新字典();
if(dictionary.ContainsKey(“someKey”))
{
var secondDictionary=dictionary[“someKey”];
if(secondDictionary.ContainsKey(“otherKey”))
{
var thirdDictionary=secondDictionary[“otherKey”];
if(第三个字典的ContainsKey(“第三个”))
{
var final=第三个字典[“第三个”];
}
}
}
如果要使用外部两个字典,则必须使用嵌套的foreach循环
差不多
var nestedDictionary = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
var foundCounter = 0;
foreach (KeyValuePair<string, Dictionary<string, Dictionary<string, string>>> midLevel in nestedDictionary)
{
var key = midLevel.Key;
if (key.Equals("WHATAMILOOKINGFOR"))
{
foundCounter++;
}
foreach (KeyValuePair<string, Dictionary<string, string>> lowerLevel in midLevel.Value)
{
if (key.Equals("WHATAMILOOKINGFOR"))
{
foundCounter++;
}
if(lowerLevel.Value.ContainsKey("WHATAMILOOKINGFOR"))
{
foundCounter++;
}
}
}
var nestedDictionary=newdictionary();
var-foundCounter=0;
foreach(嵌套字典中的KeyValuePair中级)
{
var key=middlevel.key;
if(key.Equals(“whatamilokingfor”))
{
foundCounter++;
}
foreach(中间层中的KeyValuePair lowerLevel.Value)
{
if(key.Equals(“whatamilokingfor”))
{
foundCounter++;
}
if(较低级别值ContainsKey(“WHATAMILOOKINGFOR”))
{
foundCounter++;
}
}
}
我没有在foreach上使用var,因此您可以显式地查看类型。如果您需要外部两个字典才能使用嵌套的foreach循环 差不多
var nestedDictionary = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
var foundCounter = 0;
foreach (KeyValuePair<string, Dictionary<string, Dictionary<string, string>>> midLevel in nestedDictionary)
{
var key = midLevel.Key;
if (key.Equals("WHATAMILOOKINGFOR"))
{
foundCounter++;
}
foreach (KeyValuePair<string, Dictionary<string, string>> lowerLevel in midLevel.Value)
{
if (key.Equals("WHATAMILOOKINGFOR"))
{
foundCounter++;
}
if(lowerLevel.Value.ContainsKey("WHATAMILOOKINGFOR"))
{
foundCounter++;
}
}
}
var nestedDictionary=newdictionary();
var-foundCounter=0;
foreach(嵌套字典中的KeyValuePair中级)
{
var key=middlevel.key;
if(key.Equals(“whatamilokingfor”))
{
foundCounter++;
}
foreach(中间层中的KeyValuePair lowerLevel.Value)
{
if(key.Equals(“whatamilokingfor”))
{
foundCounter++;
}
if(较低级别值ContainsKey(“WHATAMILOOKINGFOR”))
{
foundCounter++;
}
}
}
我没有在foreach上使用var,因此您可以显式地查看类型。wow。哪本字典是关键?我想检查所有三个标签…你有什么问题
.ContainsKey
不是一个很难调用的方法。。。即使你想检查内部字典,那也只是对子字典的一个foreach
。看起来你并没有尝试过任何东西。考虑重新设计你的数据结构。你不能有效地检查最里面的字典。哇。哪本字典是关键?我想检查所有三个标签…你有什么问题.ContainsKey
不是一个很难调用的方法。。。即使你想检查内部字典,那也只是对子字典的一个foreach
。看起来你并没有尝试过任何东西。考虑重新设计你的数据结构。你不能有效地检查最里面的字典。谢谢……这帮助……USER 166760,我不确定这样的数据结构是否会对你有所帮助,我希望你只是尝试测试一些代码。因为否则,此数据结构可能会导致复杂性,并且可能缺乏性能,而不是使用ContainsKey然后在索引器中查找,所以您应该执行TryGetValue;否则,索引器将在内部再次调用ContainsKey。如果你多次调用字典,尤其是递归调用,它可以极大地提高性能。谢谢…这有助于…@user1667760,我不确定这样的数据结构是否对你有帮助,我希望你只是尝试测试一些代码,因为否则,此数据结构可能会导致复杂性,并且可能缺乏性能,而不是使用ContainsKey然后在索引器中查找,所以您应该执行TryGetValue;否则,索引器将在内部再次调用ContainsKey。如果你多次调用字典,尤其是递归调用,它可以极大地提高性能。谢谢…这很有帮助…谢谢…这很有帮助。。。。