C#List或者Set集合相同的key合并Value的值

        private Dictionary<string, SortedSet<string>> MergeIdenticalKey(Dictionary<string, SortedSet<string>> dic,
            string key,
            SortedSet<string> set)
        {
            if (set == null || set.Count == 0 || string.IsNullOrEmpty(key)) return dic;
            SortedSet<string> sortedSet = null;
            if (dic.ContainsKey(key))//包含重复key
            {
                foreach (KeyValuePair<string, SortedSet<string>> item in dic)
                {
                    if (item.Key == key)
                    {
                        string strFldval = null;
                        foreach (string myValues in item.Value)
                            strFldval += myValues + ",";
                        foreach (string str in set)
                            strFldval += str + ",";
                        dic.Remove(key);
                        sortedSet = new SortedSet<string>(strFldval.Split(',').FirstOrDefault() == "" ?
                            strFldval.Split(',')
                            : strFldval.Split(',').Take(strFldval.Split(',').Length - 1)); //task(2), 去掉空字符
                        dic.Add(key, sortedSet);
                        break;
                    }
                }
            }
            else //不包含直接添加
                dic.Add(key, set);
            return dic;
        }

  

posted @ 2020-03-28 03:28  石shi  阅读(2276)  评论(0编辑  收藏  举报