我正在尝试使用此代码循环显示2个列表
foreach (string str1 in list<string>tags)
{
foreach (string str2 in list<string>ArchivedInformation) { }
}
第一个列表的标签存储为“tag1,tag2,tag3 ..”,第二个列表的信息存储为“tag1,Datestamp,0.01”,“tag2,datestamp,0.02”等。
我想问一下如何在第二个列表中获取标签并将其用作我的第一个条件?我试过拆分第二个列表,但我不能得到确切的“Tag1”作为id使用它作为一个条件。
最后,我想要做的目标是Str1(from tags list) == Str2(From Archivedinformation)
。
答案 0 :(得分:1)
一切皆有可能。其他的事情是,如果它甚至是理智的:)
public void Something()
{
// Using Dictionary
var dict = new Dictionary<string, string>();
dict.Add("tag1", "tag1,datestamp,0.01");
dict.Add("tag2", "tag2,datestamp,0.02");
// out -> "tag2,datestamp,0.02"
System.Diagnostics.Debug.WriteLine(dict["tag2"]);
// Using two separate lists
var tags = new List<string> { "tag1", "tag2" };
var infos = new List<string> { "tag1,datestamp,0.01", "tag2,datestamp,0.02" };
// out -> tag1,datestamp,0.01 & tag2,datestamp,0.02
tags.ForEach(tag =>
System.Diagnostics.Debug.WriteLine(
infos.First(info => info.StartsWith(tag))));
}