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

时间:2013-03-20 19:37:47

标签: c# asp.net

以下代表我的代码:

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。但我想在字符串中单独获取标题,并在字符串中单独分享内容。知道怎么做到这一点?

例如,上面的输出是(100, (Intro, My name is vimal))。 “100”表示键,“Intro”表示标题,“我的名字是vimal”表示内容。

1 个答案:

答案 0 :(得分:0)

要从Dictionary中获取值,您需要对foreach KeyValuePair进行 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); } ,如下所示:

{{1}}