美文网首页
c#中Dictionary

c#中Dictionary

作者: 黑哥聊dotNet | 来源:发表于2018-12-27 11:24 被阅读0次

最近在做一个小Demo发现使用Dictionary<string,list<string>>这样比较简单一点,但是查看了一下基本上介绍这种的几乎为零。所以开了这篇文章。

这篇文章的逻辑主要是添加以一个对象所拥有的行为,添加时不能添加重复,有遇见这种问题的小伙伴可以看下代码

Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();

 List<string> lst = null;

while (!rcdset.IsEOF && !rcdset.IsEmpty)//循环获取

    {

                int bTemp = 0;//用来记录key值是否重复

                rcd = rcdset.Att;

                string strTmpClass = rcd.GetFldToStr(nFieldsClass);//取key值;

                string strTmpName = rcd.GetFldToStr(nFieldsName);

                if(lst==null)//添加第一个泛型

                {

                    lst = new List<string>(strTmpName.Split(','));

                    dic.Add(strTmpClass, lst);

                }

                else

                {

                    foreach(var key in dic.Keys)

                    {

                        if (key == strTmpClass)

                        {

                            bTemp++;//记录当前key是否有多次出现

                        }

                    }

                    if(bTemp==0)//没有直接添加上去

                    {

                        lst = new List<string>(strTmpName.Split(','));

                        dic.Add(strTmpClass, lst);

                    }

                    else//有则将strTmpName 添加到当前相同的key值

                    {

                      foreach (KeyValuePair<string, List<string>> ergodic in dic)

                      {

                          if (ergodic.Key == strTmpClass)

                          {

                              string strFldval = null;

                              foreach (var myValues in ergodic.Value)

                              {

                                  strFldval += myValues + ",";

                              }

                              strFldval += strTmpName;

                              dic.Remove(strTmpClass);

                              lst = new List<string>(strFldval.Split(','));

                              List<string> strLst = lst.Distinct().ToList();//去重

                              dic.Add(strTmpClass, strLst);

                              break;

                          }

                      }

                    }

                }

相关文章

网友评论

      本文标题:c#中Dictionary

      本文链接:https://www.haomeiwen.com/subject/wxhrlqtx.html