Ari*_*ule 0 c# idictionary oledbdatareader
我正在尝试填充一个字典,其中唯一的主题值具有应与其匹配的各种代码值.
CODE SUBJECT
7DIM-062 Recruitment and Selection
7DIM-063 Recruitment and Selection
7DIM-064 Recruitment and Selection
7DIM-065 Recruitment and Selection
7DIM-066 Recruitment and Selection
7DIM-067 Recruitment and Selection
7DIM-068 Recruitment and Selection
Run Code Online (Sandbox Code Playgroud)
所以我想要的只是将Reqruitment和Selection作为唯一键添加到Dictionary中,然后将所有相应的代码添加到List中.我该怎么做呢?
Dictionary<string, List<string>> dict = new Dictionary<string,List<string>>();
Run Code Online (Sandbox Code Playgroud)
这是我的疑问
OleDbDataReader dbReader = cmd.ExecuteReader();
while (dbReader.Read())
{
string code = (string)dbReader["CODE"];
string subject = (string)dbReader["SUBJECT"];
//???? this is the point where I would want to add the values
dict.Add(subject, new List<string>().Add(code);
Run Code Online (Sandbox Code Playgroud)
首先检查你的字典是否已经有密钥,如果没有List初始化则添加新密钥.
if (!dict.ContainsKey(subject))
{
dict[subject] = new List<string>();
}
dict[subject].Add(code);
Run Code Online (Sandbox Code Playgroud)