DataTable Key/Value columns to Dictionary
There are undoubtedly more elegant ways to do this using LINQ, but to quickly and easily take the key (int) and value (string) columns from a DataTable and turn them into a Dictionary<int, string>, the following method has come in handy recently:
public static Dictionary<int, string> GetAsDictionary(DataTable data, string keyField, string valueField) {
Dictionary<int, string> dictionary = new Dictionary<int, string>();
foreach (DataRow row in data.Rows)
{
dictionary.Add(Convert.ToInt32(row[keyField]), Convert.ToString(row[valueField])); } return dictionary;
}