I have several keys (which represents subjects) in a dictionary and with that I associate several values(codes) per key.

CODE SUBJECT

7DIM-039 Communication

7DIM-040 Communication

7DIM-041 Communication

7DIM-042 Communication

7DIM-043 Communication

in order to associate eache subject(key) with the several values(code) i do this in my query. I created a dictionary

Dictionary<string, List<string>> dict = new Dictionary<string,List<string>>();

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

                if (!dict.ContainsKey(subject))
                {
                    dict.Add(subject, new List<string> { code });
                }
                else
                {
                    dict[subject].Add(code);
                }
            }

so this is the result

enter image description here

The problem I have is some of these keys are seperated by semi colons(so there are several items per key) so naturaly I have to do a split.

example: GenSubject;Careers; Listening Skills; Personal Development; Questioning Skills; Coaching/Mentoring etc.

How can I split these values and still make sure that the various code values are associated to each split value?

foreach(var kvp in dict)
        {
            foreach (var s in kvp.Key)
            {
                //splitting the subject keys?
            }
        }

I have also deviced a split method

static string[] SplitStringAt(string splitItem, char character)
    {
        return splitItem.Split(character);
    }

This is an example CODE GenSubject

7DIM-062 Communication, Questioning Skills, Decision-making

7DIM-063 Communication, Questioning Skills, Decision-making

7DIM-064 Communication, Questioning Skills, Decision-making

7DIM-065 Communication, Questioning Skills, Decision-making

7DIM-066 Communication, Questioning Skills, Decision-making

7DIM-067 Communication, Questioning Skills, Decision-making

so what I want to achieve is all the codes to be 'stored for Comunication, and the the same codes for Questioning Skills etc. but of course only once.

有帮助吗?

解决方案

Try this...

        foreach (KeyValuePair<string, List<string>> kvp in dic)
        {
            if (kvp.Key.Contains(";"))
            {
                var lst = kvp.Value;
                foreach (string subKey in kvp.Key.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries))
                {
                    dic[subKey] = lst;
                }
            }
        }

All The subKey needs to be unique!

其他提示

Sorry, your question contains some parts I do not understand: You are referring to keys and semicolon but then give an example with subjects separated by a comma.

I will assume you have subjects separated by a comma.

If you want to correctly assign your subjects whilst reading, split your subject and assign each single item to the dictionary as you did with the whole subject before.

Dictionary<string, List<string>> dict = new Dictionary<string,List<string>>();

while (dbReader.Read())
{
  string code = (string)dbReader["CODE"];
  string subject = (string)dbReader["SUBJECT"];
  foreach (string singleSubject in subject.Split(','))
  {
    if (!dict.ContainsKey(singleSubject))
    {
      dict.Add(subject, new List<string> { code });
    }
    else
    {
      dict[subject].Add(code);
    }
 }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top