一个 MainDictionary c# 中的多个词典

Multiple Dictionaries in one MainDictionary c#

我希望你能就我的代码问题给我一个提示。 我有一个 DataGridView,它从 Excel 文件中获取数据。 现在要在其中获取结构,我想创建组(dsdslls 中的关键字)并将 valueNames(dsdslls 的值和 dslls 中的关键字)添加到该组并将该内容(dslls 的值)作为 KeyValuePair 添加到 valueNames。

我的问题不是添加所有这些东西,而是将其取回。

这是代码(添加内容):

    internal Dictionary<String, Dictionary<String, LinkedList<String>>> BuildList(DataGridView dgv)
    {
        //create the main Dictionary
        Dictionary<String, Dictionary<String, LinkedList<String>>> dsdslls = new Dictionary<String, Dictionary<String, LinkedList<String>>>();
        String groupName = "Project data", value = "", valueName = "", text = "";
        //run through the whole list 
        for (int i = 0; i < dgv.RowCount - 1; i++)
        {
            //create new Dictionary for the Content
            Dictionary<String, LinkedList<String>> dslls = new Dictionary<String, LinkedList<String>>();
            //check if the String at i is a groupName, if so add it to groupName
            if (isGroupName(dgv, i))
            {
                groupName = dgv.Rows[i].Cells[0].Value.ToString();
                text = "Adding in group: " + groupName + " to value: ";
            }
            //now go forward in the list until you next Cell is empty or the list ended
            do
            {
                //check if the String at i is a valueName, if so add it to valueName
                if (isValueName(dgv, i))
                {
                    //create the LinkedList for units and values
                    LinkedList<String> lls = new LinkedList<String>();
                    valueName = dgv.Rows[i].Cells[0].Value.ToString();
                    //check if group and valuename are NOT the same
                    if (isNotSame(valueName, groupName))
                    {
                        //now run the colums for units and values and add them to the List until you reach the end of used columns
                        int j = 0;
                        do
                        {
                            value = dgv.Rows[i].Cells[1 + j].Value.ToString();
                            lls.AddLast(value);
                            if (j == 0)
                            {
                                text += "\n" + valueName + " in (" + lls.First.Value.ToString() + "): ";
                            }
                            else
                            {
                                text += lls.Last.Value.ToString();
                            }
                            j++;
                        } while (j < dgv.Rows[i].Cells.Count - 1);
                        //add the valueName and List as new keyvaluepair to the dictionary.
                        dslls.Add(valueName, lls);
                    }
                }
                i++;
            } while (!isStringEmpty(dgv.Rows[i].Cells[0].Value.ToString()) && i < dgv.RowCount - 1);
            //show added content
            MessageBox.Show(text);
            //check if main dictionary contains the latest groupname, if not add the groupName and the last dictionary to the main dictionary
            if (!dsdslls.ContainsKey(groupName))
            {
                dsdslls.Add(groupName, dslls);
            }
        }
        MessageBox.Show("Building successfully finished.");
        return dsdslls;
    }

我没有将正确的内容返回到指定的 groupName...例如:"groupName = "Project Data" 我返回了组的内容:"Electrical Network",这是下一个关键字在主词典中

现在是获取数据的代码:

           internal void /*Dictionary<String, LinkedList<String>>*/ GetGroupContent(Dictionary<String, Dictionary<String, LinkedList<String>>> dsdslls, String groupName)
    {
        //Dictionary<String, LinkedList<String>> dslls = new Dictionary<String, LinkedList<String>>();
        String groupN = "", text = "";
        //Check if Dictionary contains groupName
        if (dsdslls.ContainsKey(groupName))
        {
            //run through the dictionary
            foreach (var s in dsdslls)
            {
                //give back the keyword (just for the MessageBox)
                if (s.Key == groupName)
                {
                    groupN = s.Key;
                }
                else
                {
                    //run throught the little dictionary to get the keywords from it. 
                    foreach (var t in s.Value)
                    {
                        text += t.Key + "\n";
                    }
                }
            }
            MessageBox.Show("Content of Group " + groupN + ": \n" + text);
            text = "";
        }
        //return dslls;
    }

亲切的问候

米尔科

很难理解您对这段代码的期望,因为主要问题没有得到很好的描述。

总之,看来你的数据检索逻辑可能有问题。 如果您想获取具有匹配名称的组的数据,那么您必须移动 if 语句的 else 部分。只有找到具有正确名称的组时,您才需要进行文本连接。

...
//give back the keyword (just for the MessageBox)
if (s.Key == groupName)
{
    groupN = s.Key;

    //run throught the little dictionary to get the keywords from it. 
    foreach (var t in s.Value)
    {
        text += t.Key + "\n";
    }
}
...