嵌套Dictionary中相同value放在一起

來源:互聯網
上載者:User

 問題:Dictionary<Dictonary<key,value>,value2>

如何根據相同value放在一起啊,key,value,value2都是string類型

<<"2", "c Item">,"df">
<<"6", "a Item">,"ad">

<<"1", "c Item">,"jk">

 

結果:

<<"2", "c Item">,"df">

<<"1", "c Item">,"jk">
<<"6", "a Item">,"ad">

using System;using System.Collections.Generic;using System.Text;namespace 對Dictionary的Value排序功能實現{         class Program    {               static void Main(string[] args)        {            Dictionary  <string, string> dic1 = new Dictionary  <string, string>();            dic1.Add("2", "c Item");            Dictionary  <string, string> dic2 = new Dictionary  <string, string>();            dic2.Add("6", "c Item");            Dictionary  <string, string> dic3 = new Dictionary  <string, string>();            dic3.Add("1", "a Item");            Dictionary<string, string> dc = new Dictionary<string, string>();            dc.Add("2", "c Item");            dc.Add("6", "a Item");            dc.Add("1", "c Item");            //取得value的非重複值,為了方便不從dic2List中取            List<string> va = new List<string>();            foreach (KeyValuePair<string, string> kvp in dc)            {                                string valueWith = kvp.Value;                if (!va.Contains(valueWith))                {                    va.Add(valueWith);                }            }            Dictionary<Dictionary<string, string>, string> dic2list = new Dictionary<Dictionary<string, string>, string>();            dic2list.Add(dic1, "df");            dic2list.Add(dic2, "ad");            dic2list.Add(dic3, "jk");                        Dictionary<Dictionary<string,string>,string> sortKeyValue=new Dictionary<Dictionary<string,string>,string>();                        Dictionary<Dictionary<string, string>,string>.KeyCollection keyColl = dic2list.Keys;            //value的非重複值迴圈,相等的放在一起            for (int i = 0; i < va.Count;i++ )            {                               string strList = va[i];                foreach (Dictionary<string, string> keyStr in keyColl)                {                    Dictionary<string, string> keyOneValue = new Dictionary<string, string>();                    string value2 = dic2list[keyStr];                    Dictionary<string, string>.KeyCollection keyColl2 = keyStr.Keys;                    Dictionary<string, string>.ValueCollection valueColl2 = keyStr.Values;                    string ssk = string.Empty;                    string ssv = string.Empty;                    foreach (string sk in keyColl2)                    {                        ssk = sk;                    }                    foreach (string sv in valueColl2)                    {                        ssv = sv;                    }                    if (ssv.Equals(strList))                    {                        keyOneValue.Add(ssk, ssv);                        sortKeyValue.Add(keyOneValue, value2);                    }                }            }              Console.ReadKey();        }    }}

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.