如何动态地为字典添加和命名键值对? C#

时间:2018-03-12 14:51:51

标签: c# .net

我创建了一个名为' sGC'它有一个字符串键和一个包含2个字符串列表的元组值。

Dictionary<string, Tuple<List<string>, List<string>>> sGC = new Dictionary<string, Tuple<List<string>, List<string>>>();

我想向此字典添加新密钥,这些密钥是来自DataTable DataRow(DR)的串联字符串。如果满足某个条件,那么来自DR的字符串将进入元组的Item1或Item2。

此代码正在循环遍历DataTable的foreach循环中执行,如果行符合if语句标准,则停止在某些行上。

var dicTup = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
dicTup.Item2.Add(DR["PupilID"].ToString());
sGC.Add(DR["CSN"].ToString() + DR["AW2"].ToString(), dicTup);

这是将新的动态命名密钥添加到字典中的最佳方法吗?

我相信这个JavaScript主题的最佳答案是我在C#中寻找的答案:How to create dictionary and add key–value pairs dynamically?

以下完整代码。

foreach (DataRow DR in MainData.DataTable.Rows)
            {   
                //Rows containing a symbol mark score
                if ((DR["CN"].ToString() == "LC") && (DR["AW2"].ToString() != ""))
                {
                    //Store male results
                    //If the Subject Name + Level Code is already a key in the dictionary, append to Tuple List 1
                    //If key does not exist in Dictionary, create new DictKey and value
                    if (DR["PG"].ToString() == "Male")
                    {                                           
                        if (sGC.ContainsKey(DR["CSN"].ToString() + DR["AW2"].ToString()))
                        {                           
                            sGC[DR["CSN"].ToString() + DR["AW2"].ToString()].Item1.Add(DR["PID"].ToString());
                        }                           

                        else
                        {
                            var dicTup = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
                            dicTup.Item1.Add(DR["PID"].ToString());
                            sGC.Add(DR["CSN"].ToString() + DR["AW2"].ToString(), dicTup);                           
                        }
                    }

                    //Store female results
                    //If the Subject Name + Level Code is already a key in the dictionary, append to Tuple List 2
                    //If key does not exist in Dictionary, create new DictKey and value                     
                    if (DR["PG"].ToString() == "Female")
                    {                                           
                        if (sGC.ContainsKey(DR["CSN"].ToString() + DR["AW2"].ToString()))
                        {                               
                            sGC[DR["CSN"].ToString() + DR["AW2"].ToString()].Item2.Add(DR["PID"].ToString());           
                        }                           

                        else
                        {
                            var dicTup = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
                            dicTup.Item2.Add(DR["PupilID"].ToString());
                            sGC.Add(DR["CSN"].ToString() + DR["AW2"].ToString(), dicTup);
                        }                                                   
                    }                                                                       
                }

新编辑和格式化的代码:

    private void storeMarkSheetData()
    {   
        if (MainData.DataTable != null)
        {

            if(subjectGradeCounts.Count == 0)
            {
                foreach (DataRow DR in MainData.DataTable.Rows)
                {     
                    string cN   = DR["ColumnName"].ToString();
                    string aW2  = DR["AssessmentAwarded2"].ToString();
                    string cSN  = DR["ClassSubjectName"].ToString();
                    string pID  = DR["PupilID"].ToString();
                    string pG   = DR["PupilGender"].ToString();

                    //Rows containing a symbol mark score
                    if ((cN == "Level Code") && (aW2 != ""))
                    {
                        //Check to see if the dictionary contains the key, adds it if not
                        if(!subjectGradeCounts.ContainsKey(cSN + aW2))
                        {
                            subjectGradeCounts.Add(cSN+aW2, new 
                                Tuple<List<string>, List<string>>(new List<string>(), new 
                                List<string>()));
                        }

                        //Now that the key exists, if it didn't previously
                        //If male add to list 1, else list 2 (for female)
                        if(pG == "Male")
                        {
                            subjectGradeCounts[cSN + aW2].Item1.Add(pID);
                        }
                        else
                        {
                            subjectGradeCounts[cSN + aW2].Item2.Add(pID);
                        }
                    }
                }
            }
        }
    }

谢谢大家。

1 个答案:

答案 0 :(得分:0)

这里我简化了你要检查密钥是否存在的内容,如果没有,则将其添加到新的初始化列表中,然后如果男性添加到列表1,则执行一个其他操作(女性)添加到列表2,从您发布的代码是我提出的

 foreach (DataRow DR in MainData.DataTable.Rows)
 {   
         //Rows containing a symbol mark score
         if ((DR["CN"].ToString() == "LC") && (DR["AW2"].ToString() != ""))
         {
            //Check to see if your dictionary contains the key, if not, add it
            if(!sGC.ContainsKey(DR["CSN"].ToString() + DR["AW2"].ToString()))
            {
                 sGC.Add(DR["CSN"].ToString() + DR["AW2"].ToString(), new 
                 Tuple<List<string>,List<string>>(new List<string>(), new 
                 List<string>()));
            }

            //Now that the key exists, if it didn't previously
            //If male add to list 1, else list 2 (for female)
            if(DR["PG"].ToString() == "Male")
            {
                 sGC[DR["CSN"].ToString() + DR["AW2"].ToString()].Item1.Add(DR["PupilID"].ToString());
            }
            else
            {
                 sGC[DR["CSN"].ToString() + DR["AW2"].ToString()].Item2.Add(DR["PupilID"].ToString());
            }
        }
    }