首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套的ConcurrentDictionary

嵌套的ConcurrentDictionary
EN

Stack Overflow用户
提问于 2014-04-08 20:39:19
回答 1查看 1.3K关注 0票数 0

我想在解析一个大的XML文件后创建一个嵌套的ConcurrentDictionary。运行下面的代码时,我得到的结果是最里面的(TmpDictionaryDid)字典是空的。

我猜我没有正确地保存字典或错误地加载它们,但是我找不出哪些是不正确的。

有没有可能用ConcurrentDictionary解决这个问题,或者我应该尝试用另一个解决方案?

代码语言:javascript
复制
public void temp22db()
    {
        ConcurrentDictionary<string, ConcurrentDictionary<string, string>> dictionary22 = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>();
        try
        {
            ConcurrentDictionary<string, string> did = new ConcurrentDictionary<string, string>();

            did.TryAdd("F186 1", "test1");
            did.TryAdd("F186 2", "test2");
            dictionary22.TryAdd("F186", did);

            did.TryAdd("EDA0 1", "test3");
            did.TryAdd("EDA0 2", "test4");
            dictionary22.TryAdd("EDA0", did);

            string test;
            did.TryGetValue("EDA0 1", out test);
            Helper.logInWindow("This works " + test);
        }
        catch (Exception e)
        {
            Helper.logInWindow("SDDBTemp: " + e.Message);
        }
        ecuDictionary2.TryAdd("1638", dictionary22);
    }

public string getDIDInterpretation()
    {
        string outStr = "";
        ConcurrentDictionary<string, ConcurrentDictionary<string, string>> tmpDictionary;
        ecuDictionary2.TryGetValue("1638", out tmpDictionary);

        if (tmpDictionary != null)
        {
            ConcurrentDictionary<string, string> tmpDictionaryDid;
            tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid);

            if (tmpDictionaryDid != null)
            {
                tmpDictionaryDid.TryGetValue("EDA0 1", out outStr);
            }
        }

        Helper.logInWindow("Why U no work? " + outStr);
        return outStr;
    }
EN

回答 1

Stack Overflow用户

发布于 2015-04-09 21:02:16

您应该这样使用它:(父ConcurrentDictionary将允许内部字典的并发性)

代码语言:javascript
复制
    public static void temp22db()
    {
        Dictionary<string, Dictionary<string, string>> dictionary22 = new Dictionary<string, Dictionary<string, string>>();
        try
        {
            Dictionary<string, string> did = new Dictionary<string, string>();

            did.Add("F186 1", "test1");
            did.Add("F186 2", "test2");
            dictionary22.Add("F186", did);

            did.Add("EDA0 1", "test3");
            did.Add("EDA0 2", "test4");
            dictionary22.Add("EDA0", did);

            string test;
            did.TryGetValue("EDA0 1", out test);
            Helper.logInWindow("This works " + test);
        }
        catch (Exception e)
        {
            Helper.logInWindow("SDDBTemp: " + e.Message);
        }
        ecuDictionary2.TryAdd("1638", dictionary22);
    }

    public static string getDIDInterpretation()
    {
        string outStr = "";
        Dictionary<string, Dictionary<string, string>> tmpDictionary;
        ecuDictionary2.TryGetValue("1638", out tmpDictionary);

        if (tmpDictionary != null)
        {
            Dictionary<string, string> tmpDictionaryDid;
            tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid);

            if (tmpDictionaryDid != null)
            {
                tmpDictionaryDid.TryGetValue("EDA0 1", out outStr);
            }
        }

        Helper.logInWindow("Why U no work? " + outStr);
        return outStr;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22937294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档