首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何动态地向字典中添加和命名键值对?C#

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

Stack Overflow用户
提问于 2018-03-12 14:51:50
回答 1查看 1.1K关注 0票数 0

我创建了一个名为“sGC”的字典,它有一个字符串键和一个包含两个字符串列表的元组值。

代码语言:javascript
复制
Dictionary<string, Tuple<List<string>, List<string>>> sGC = new Dictionary<string, Tuple<List<string>, List<string>>>();

我想向这个字典中添加新的键,这些键是来自DataTable DataRow (DR)的连接字符串。如果满足了某个标准,那么来自DR的字符串将进入元组的Item1或Item2中。

这段代码是在foreach循环中执行的,循环遍历DataTable,如果该行符合if语句条件,则在某些行上停止。

代码语言:javascript
复制
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?中寻找的答案。

下面是完整代码。

代码语言:javascript
复制
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);
                        }                                                   
                    }                                                                       
                }

新编辑和格式化的代码:

代码语言:javascript
复制
    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);
                        }
                    }
                }
            }
        }
    }

谢谢你们所有人。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-12 15:08:15

在这里,我简化了您只需检查键是否存在的问题,如果它没有将其添加到新的初始化列表中,那么,如果男性添加到列表1中(女性),从您发布的代码中添加到列表2,这就是我想出的。

代码语言:javascript
复制
 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());
            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49238168

复制
相关文章

相似问题

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