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

本文关键字:字符串 三个 拆分 字典 元组 | 更新日期: 2023-09-27 18:04:51

下面是我的代码:

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();

valueIntroduction.InnerHtml中,它既有标题也有内容。现在我想分别拆分为Header和Content。但我想让header分别在字符串中得到内容也分别在字符串中得到。有什么办法吗?

Eg以上输出为(100, (Intro, My name is vimal))。"100"代表key, "Intro"代表Header, "My name is vimal"代表Content。

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

要获得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);
        }