Search code examples
c#splitidictionary

Splitting value items in a dictionary


I asked a similar question before but am just asking it from a slightly different angle.

I have a list of codes and underneath each code I have a bunch of values that are taken from the fields Subject and GenSubject.

Problem is these values(especially the general subjects) contain various elements, seperated by ';'. How can I incorperate the split function within the dictionary as to split the various values as I run the query?

while (dbReader.Read())
            {
                string code = (string)dbReader["CODE"];
                string subject = (string)dbReader["SUBJECT"];
                string generalSublject = (string)dbReader["GenSubject"];


                dict.Add(code, new List<string> { subject,generalSublject });


            }

This is the result of which I want to split the values wherever they need splitting.

enter image description here

(I want to split the values before adding them to the dictionary)

Thank you


Solution

  • Just split the generalSublject string and create the list based on it, then insert the subject as the first item.

    var list = new List<string>(generalSublject.Split(new string[] { ";" },
        StringSplitOptions.None));
    list.Insert(0, subject);
    dict.Add(code, list);