C#:嵌套字典问题

时间:2012-08-07 08:17:12

标签: c# dictionary

我有下面的代码,用于构建具有以下构造Dictionary<string<Dictionary<string, string>的字典对象。出于某种原因,每次我向其添加项目时,关键文本都是正确的,但值(字典)会覆盖前一个。这可能更好地解释如下:

迭代1

  • key1,dictionary1

迭代2

  • key1,dictionary2
  • key2,dictionary2

迭代3

  • key1,dictionary3

  • key2,dictionary3

  • key3,dictionary3

导致这种情况的原因是什么?我如何修复此代码以阻止它覆盖每个条目中的字典?

QueryNameUserQueryString = new Dictionary<string, string>();
DialectDictionary = new Dictionary<string, Dictionary<string, string>>();

while (dataBaseConnection.NextRecord())
                {
                if (QueryNameUserQueryString != null)
                    {
                    QueryNameUserQueryString.Clear();
                    }

                string dialect = dataBaseConnection.GetFieldById (0);

                //If no dialect then carry out next iteration
                if (String.IsNullOrEmpty (dialect))
                    {
                    continue;
                    }

                try
                    {
                    dataBaseConnection2.ExecutePureSqlQuery ("SELECT * FROM " + sqlFactoryTable + " WHERE SQL_FACTORY_DIALECT = '" + dialect + "'");
                    }
                catch
                    {
                    dataBaseConnection.Close();
                    dataBaseConnection2.Close();

                    throw;
                    }

                //Do we have query strings for this dialect?
                if (!dataBaseConnection2.HasRows())
                    {
                    continue;
                    }

                //loop through query strings
                while (dataBaseConnection2.NextRecord())
                    {
                    string queryName = dataBaseConnection2.GetFieldById (2);
                    string queryString = dataBaseConnection2.GetFieldById (3);
                    string user = dataBaseConnection2.GetFieldById (4);

                    //create composite key for dictionary
                    string compositeKey = dialect + "," + queryName + "," + user;

                    if (QueryNameUserQueryString != null)
                        {
                        //Construct our query string dictionary
                        QueryNameUserQueryString.Add (compositeKey, queryString);
                        }
                    }

                //If we have a query string dictionary
                if (QueryNameUserQueryString != null)
                    {
                    //Ensure m_dialect dictionary is not null
                    if (DialectDictionary != null)
                        {
                        //Construct our dictionary entry for this dialect
                        DialectDictionary.Add (dialect, QueryNameUserQueryString);
                        }
                    }
                }
            }

2 个答案:

答案 0 :(得分:4)

您似乎在每次迭代时都使用相同的QueryNameUserQueryString实例。当它被添加到DialectDictionary时,它会被添加为参考 - 而不是原始的副本。

要“正确”解决问题,我会在while-scope中移动QueryNameUserQueryString变量的声明。这样你就可以确保它只能存在于单个迭代的范围内,而不是存在于几个迭代的范围内。当它被添加到DialectDictionary时,该引用会存在于该字典中,您可以安全地离开该范围。

答案 1 :(得分:2)

每次都使用QueryNameUserQueryString的相同实例。取代

QueryNameUserQueryString.Clear();

QueryNameUserQueryString = new Dictionary<string, string>();