C# 如何在C语言中处理嵌套字典中的数据#
Im已按如下方式设置嵌套字典:C# 如何在C语言中处理嵌套字典中的数据#,c#,dictionary,collections,C#,Dictionary,Collections,Im已按如下方式设置嵌套字典: static Dictionary<string, Dictionary<UInt32, TClassType> > m_dictionary = new Dictionary<string, Dictionary<UInt32, TClassType>>(); TClassType newEntry = new TClassType(s_title, ui_code, s_address) if (! m_dic
static Dictionary<string, Dictionary<UInt32, TClassType> > m_dictionary = new Dictionary<string, Dictionary<UInt32, TClassType>>();
TClassType newEntry = new TClassType(s_title, ui_code, s_address)
if (! m_dictionary.ContainsKey(s_title))// check as the same s_title can occur multiple times but have different ui_code and s_address values
{
m_dictionary.Add(s_title, new Dictionary<uint, TClassType>());
}
m_dictionary[s_title].Add(ui_code, s_address);
静态字典m_Dictionary=newdictionary();
“TClassType”包含三个属性:
- 标题(字符串)
- 代码(uint)
- 地址(字符串)
static Dictionary<string, Dictionary<UInt32, TClassType> > m_dictionary = new Dictionary<string, Dictionary<UInt32, TClassType>>();
TClassType newEntry = new TClassType(s_title, ui_code, s_address)
if (! m_dictionary.ContainsKey(s_title))// check as the same s_title can occur multiple times but have different ui_code and s_address values
{
m_dictionary.Add(s_title, new Dictionary<uint, TClassType>());
}
m_dictionary[s_title].Add(ui_code, s_address);
TClassType newEntry=新的TClassType(s_标题、ui_代码、s_地址)
if(!m_dictionary.ContainsKey(s_title))//检查相同的s_title可能出现多次,但具有不同的ui_代码和s_地址值
{
m_dictionary.Add(s_title,newdictionary());
}
m_字典[s_标题]。添加(ui_代码,s_地址);
现在我的问题是,访问特定键[s_title]的所有值的好方法是什么
键[s_title]将包含嵌套字典中的许多项,对于唯一的[s_title]条目,我希望从嵌套字典中获取与此outter键相关的所有相关键和值
抱歉,我希望这不会让人困惑,我发现这和我试图实现的一样难
提前谢谢大家试试这个:
if (m_dictionary.Contains(s_title))
foreach(TClassType entry in m_dictionary[s_title].Values)
// Do something with the entry.
你以前用过linq吗?这将是groupby的主要用途。你所做的增加了一个层次的回旋 在当前设置中,如果有TClassType列表,则可以使用linq where表达式仅获取具有要查找的标题的内容,然后获取所需的ui_代码 例如编辑(我以前在手机上,很难在上面编码:))
IEnumerable entries=entriesList//无论您使用什么来填充条目
var titles=entries.Where(x=>x.s_title==“您正在寻找的标题”).Distinct();
您所说的“访问的好方法”是什么意思?使用上下文是什么?首先,你的例子是把事情搞混了。您正在检查键s\u title
是否存在,然后添加键s\u type
的条目。请修理/清理。那么s_key
从何而来?因此您尝试为您的TClassType创建索引,首先按标题分组,然后按代码分组?感谢您所做的更改。现在请修改你的文本。在你的问题中,你指的是s_type
和s_key
,这在你的示例代码中从未出现过。抱歉,但我希望现在它更准确一点:)我还没有,但我只是调查了一下,看起来非常有用:)谢谢你当我回答人们我试图在他们的问题范围内给出答案时,而且可能在他们的知识范围内。当然,LINQ可以提供帮助,但提供新技术可能会使情况变得更加复杂,甚至可能不会产生预期的结果(例如:LINQ可能会降低性能)。这可能是真的,其他人也在这样做,但不提供替代方案会限制潜在的知识库。我个人知道,我喜欢被介绍给其他选项,因为我学到了更多,是的,它可以混淆最初的问题,但学习其他技术会提供额外的工具集。正如您所说,Linq可以降低速度,但大型嵌套词典也可以。太好了,非常感谢您的时间和帮助:)