There are a number of ways you could take a List<CANBusColorMsgIdMap> and go through it, and construct, for each unique MsgId in it, a List<Color>.
But, I think that's the wrong strategy: I suggest you create the data structures to hold msgId and a List<Color> once in a static Class, and then, in the constructor of your CANBusColorMsgIdMap Class, "do the right thing" to update those data structures.
The outcome will be much simpler: you'll just take the existing data stored in the "tracking" Class, and write that information out to your log file.
Here's a very rough sketch based on your last posted Class definition: The static class contains a Dictionary<string,List<Color>>, and one method to update that Dictionary:
public static class CanBusColorTracker
{
public static Dictionary<string, List<Color>> dctMsgIDColors = new Dictionary<string, List<Color>>();
public static void Track(Color buscolor, string msgId)
{
if (! dctMsgIDColors.Keys.Contains(msgId))
{
dctMsgIDColors.Add(msgId, new List<Color>());
}
if (! dctMsgIDColors[msgId].Contains(buscolor))
{
dctMsgIDColors[msgId].Add(buscolor);
}
}
}
public class CANBusColorMsgIdMap
{
public string msgId;
public Color BusColor { get; set; }
public string MsgId
{
get
{
if (!String.IsNullOrEmpty(msgId))
return msgId.ToUpper();
else
return string.Empty;
}
set { msgId = value; }
}
public CANBusColorMsgIdMap(Color buscolor, string msgId)
{
CanBusColorTracker.Track(buscolor, msgId);
this.BusColor = buscolor;
this.msgId = msgId;
}
}
Assuming you get a valid reference to the Dictionary in the static Class in the right place, you can easily enumerate it with code like this:
foreach (var kvp in CanBusColorTracker.dctMsgIDColors)
{
Console.Write(kvp.Key + " ");
foreach (var clr in kvp.Value)
{
Console.Write(clr + " ");
}
Console.WriteLine();
}
That code would generate output like this in VS' Output window:
CFOO400 Color [Red] Color [Yellow] Color [Green]
18FFEEC Color [Yellow]
6CEEFEE Color [Red]
C00000B Color [Green] Color [Yellow]
Note that you can use a dynamic Class, rather than a static one; just make sure you create one instance of it, and do the right thing so your constructor of your Class can access it.