2014-09-26 53 views
0

我通过2所列出试图循环使用此代码如何使用每个

foreach (string str1 in list<string>tags) 
{ 
    foreach (string str2 in list<string>ArchivedInformation) { } 
} 

第一个列表有被存储为标签比较字符串的2名列表“标签1,标签2,标签3 ..”和第二个列表的信息存储为“tag1,Datestamp,0.01”,“tag2,datestamp,0.02”等等。

我想问一下如何获得第二个列表中的标签并将它作为我的第一个列表的条件?我已经尝试拆分第二个列表,但我无法得到确切的“Tag1”作为一个ID使用它作为条件。

最后我想要做的目标是Str1(from tags list) == Str2(From Archivedinformation)

+4

你有没有考虑过使用字典? – 2014-09-26 17:58:15

+1

我没有,谢谢你的建议。 – Eman 2014-09-26 18:00:17

+1

只想知道,这只是使用只是字典?或者可以使用列表来完成 – Eman 2014-09-26 18:06:05

回答

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)))); 
    } 
+0

谢谢!我试图使用这个功能 string cmp; cmp = str2.Substring(0,str2.IndexOf(',')); – Eman 2014-09-26 20:00:09

相关问题