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;
        }

  

猜你喜欢

转载自www.cnblogs.com/sunliyuan/p/12585498.html