简体   繁体   English

如何在C#中将带有元组的Dictionary拆分为三个不同的字符串?

[英]How to split Dictionary with tuples into three different strings in C#?

The following represents my code: 以下是我的代码:

Dictionary<string, Tuple<string, string>> headCS = new Dictionary<string, Tuple<string, string>> { };

while (results.Read())
{
    headCS.Add(results["ID"].ToString(),
               new Tuple<string, string>(results["TEXT_HEADER"].ToString(), 
                                         results["TEXT_CONTENT"].ToString()));
}

valueIntroduction.InnerHtml = headCS.Values.ElementAt(0).ToString();

In valueIntroduction.InnerHtml , it has both header as well as content. valueIntroduction.InnerHtml ,它既包含标头也包含内容。 Now I want to split into Header separately and Content separately. 现在,我想分别分为标题和内容。 But I want to get the header separately in a string and content separately in a string. 但是我想在字符串中分别获取标头,并在字符串中分别获取内容。 Any idea how to achieve this? 任何想法如何实现这一目标?

Eg Output for the above is (100, (Intro, My name is vimal)) . 例如,上面的输出是(100, (Intro, My name is vimal)) "100" represents key, "Intro" represents Header, "My name is vimal" represents Content. “ 100”代表键,“简介”代表标题,“我的名字很生动”代表内容。

To get the values out of a Dictionary you need to do a foreach over the KeyValuePair as follows: 要从Dictionary获取值,您需要对KeyValuePair进行如下foreach

        Dictionary<string, Tuple<string, string>> headCS = new Dictionary<string, Tuple<string, string>>();

        List<string> key  = new List<string>();
        List<string> header = new List<string>();
        List<string> content = new List<string>();

        foreach (KeyValuePair<string, Tuple<string,string>> item in headCS)
        {
           //print values to console
            Console.WriteLine("{0},{1},{2}", item.Key, item.Value.Item1, item.Value.Item2);

            //store values for in another form just as an example of what they represent
            key.Add(item.Key);
            header.Add(item.Value.Item1);
            content.Add(item.Value.Item2);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM