ユーザーが文字のグループを入力し、文字が少しごちゃ混ぜになった後、データベースに保存した単語リストと比較される、スクラブル詐欺師のようなプログラムに取り組んでいます。文字の組み合わせごとにデータベースを検索するのは非常に時間がかかるため、最初に単語をリストに保存して、メモリ内で使用できるようにしました。これにより、パフォーマンスが大幅に向上しました。しかし、キーが単語の最初の文字である Hashtable の List に単語リストを格納することで、さらに高速化したいと考えていますが、どうすればよいかについて少し固執しています。
私が持っている(そして明らかにうまくいかない)のは次のとおりです。
public Hashtable populateWordTable()
{
Hashtable wordTable = new Hashtable();
List<string> wordTableWordList = new List<string>();
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;
int found = 0;
try
{
connect.Open();
dr = find.ExecuteReader();
string key = string.Empty;
string keyStartingPosition = "a";
while (dr.Read())
{
// if word is present
if (!dr.IsDBNull(0))
{
found = Convert.ToInt32(dr[0]);
key = dr[1].ToString().Substring(0, 1);
}
if (found > 0)
{
// if we have transitioned to the next letter in the alphabet
if (key != keyStartingPosition)
{
wordTable.Add(keyStartingPosition, wordTableWordList);
List<string> newList = new List<string>();
newList.Add(dr[1].ToString());
keyStartingPosition = key;
}
// still in the same letter in the alphabet
else
{
wordTableWordList.Add(dr[1].ToString());
}
}
}
}
catch (Exception ex)
{
}
finally
{
connect.Close();
}
return wordTable;
}