Ok so i have 2 lists
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
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
<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
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.