我正在尝试将英语词典(这个特定词典中大约 109,000 个单词)存储到 Dictionary> 数据结构中,但我很难弄清楚如何去做。我目前的方法想法是将单词的第一个字符存储为键值,然后将单词存储在列表(“wordlist”)中。当键从“a”变为“b”时(或“b”变为“c”等)就是我卡住的地方,因为我无法弄清楚如何处理列表。这是我的努力。非常感谢任何帮助。
public Dictionary<char, IEnumerable<string>> populateWordDictionary()
{
Dictionary<char, IEnumerable<string>> wordDictionary = new Dictionary<char, IEnumerable<string>>();
//List<string> wordList;
connect = new SqlConnection(connectionString);
SqlCommand find = new SqlCommand("Select * FROM English order by Word", connect);
// starting with first record, store each word into the List<string> wordlist
SqlDataReader dr = null;
try
{
connect.Open();
dr = find.ExecuteReader();
char key;
char keyStartingPosition = 'a';
List<string> wordList = new List<string>();
while (dr.Read())
{
// if a word is present
if (!dr.IsDBNull(0))
{
// set Dictionary key value to the first letter in the word being evaluated
key = Convert.ToChar(dr[1].ToString().Substring(0, 1));
// if the key value is not the same as the starting position, clear the list
// i.e., we've advanced to the next letter in the alphabet
if (key != keyStartingPosition)
{
// add the current list to the dictionary
wordDictionary.Add(key, wordList);
// advance key starting position to the new key value
keyStartingPosition = key;
// and clear current content of wordList
wordList.Clear();
}
// if the first letter of the word list hasn't advanced in the alphebet
// simply store the word to the current list.
if (key == keyStartingPosition)
{
wordList.Add(dr[1].ToString());
}
}
}
}
catch (Exception ex)
{
}
finally
{
connect.Close();
}
return wordDictionary;
}