[英]What am i doing wrong with my C# looping
Ok so i have 2 lists 好,所以我有2个清单
List<string> playlists
List<string> sync
and the lets say the content of playlists is three strings 假设播放列表的内容是三个字符串
{"more and you", "and us", "more"}
and the content of sync is this 同步的内容是这个
{"more and you-20120312", "more and you-20120314", "more and you-20120313", "and us-20120313", "and us-20120314", "more-20120314", "more-20120313", "more-20120312"}
Basically what i want to do is loop through all the loop through the playlists and find the associated syncs and print them out and they need to be 3 otherwise i want to color them differently..So here is my code so far 基本上,我想做的是遍历播放列表中的所有循环,找到相关的同步并打印出来,它们必须为3,否则我想为它们着色不同..所以这是到目前为止的代码
StringBuilder sb = new StringBuilder();
sb.Append("<h2>Playlist Information</h2>");
foreach (string play in playlists)
{
int counter = 0;
foreach (string s in sync)
{
if (s.StartsWith(play))
{
sb.Append("<p class=\"good\">" + s + "</p>");
counter++;
}
}
}
So i want the final html to look like this 所以我希望最终的html看起来像这样
<h2>Playlist Information</h2>
<p class=\"good\">more and you-20120312</p>
<p class=\"good\">more and you-20120313</p>
<p class=\"good\">more and you-20120314</p>
<p class=\"bad\">and us-20120313</p>
<p class=\"bad\">and us-20120314</p>
<p class=\"good\">more-20120312</p>
<p class=\"good\">more-20120313</p>
<p class=\"good\">more-20120314</p>
the bad for the items that dont meet at least 3...any ideas on how to achieve this with my code 对于不满足至少3个项目的项目不利...关于如何使用我的代码实现此目标的任何想法
It is very easy to achieve this - just build another list during your check instead of the counter and then check the size of the "innerlist". 实现此目标非常容易-只需在检查过程中建立另一个列表而不是计数器,然后检查“内部列表”的大小即可。 I called it currentSyncSet:
我称之为currentSyncSet:
static void Main(string[] args)
{
List<string> playlists = new List<string>(){"more and you", "and us", "more"};
List<string> sync = new List<string>() { "more and you-20120312", "more and you-20120314", "more and you-20120313", "and us-20120313", "and us-20120314", "more-20120314", "more-20120313", "more-20120312" };
StringBuilder sb = new StringBuilder();
sb.Append("<h2>Playlist Information</h2>\r\n");
HashSet<string> finalSyncResult = new HashSet<string>();
foreach (string play in playlists)
{
List<string> currentSyncSet = new List<string>();
foreach (string s in sync)
{
if (s.StartsWith(play))
{
currentSyncSet.Add(s);
}
}
foreach (var syncset in currentSyncSet)
{
if (currentSyncSet.Count < 3)
{
finalSyncResult.Add("<p class=\"bad\">" + syncset + "</p>");
}
else
{
finalSyncResult.Add("<p class=\"good\">" + syncset + "</p>");
}
}
}
foreach (var result in finalSyncResult)
{
sb.Append(result + "\r\n");
}
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
The Output is then: 输出为:
<h2>Playlist Information</h2>
<p class="good">more and you-20120312</p>
<p class="good">more and you-20120314</p>
<p class="good">more and you-20120313</p>
<p class="bad">and us-20120313</p>
<p class="bad">and us-20120314</p>
<p class="good">more-20120314</p>
<p class="good">more-20120313</p>
<p class="good">more-20120312</p>
Greetings 问候
Update 1: Sry, last time, i forgot, that you don't want to have duplicate entries - therefore i added a HashSet in this solution. 更新1:对不起,上一次,我忘记了,您不想重复条目-因此,我在此解决方案中添加了HashSet。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.