0

I have this dictionary Dictionary<TableKey, string> where TableKey is an enum type.

I'm trying to populate the dictionary with data from a DataSet object that I acquire during an sql query

DataSet resultSet = Utils.RunQuery(sqlQuery);

if (resultSet.Tables.Count > 0)
{
    foreach (DataRow row in resultSet.Tables[0].Rows)
    {
        // Makes the dictionary with populated keys from enum
        Dictionary<TableKey, string> dic = new Dictionary<TableKey, string>();
        foreach (TableKey key in Enum.GetValues(typeof(TableKey)))
            dic.Add(key, "");

        // the foreach loop in question, which should insert row data into the dic
        foreach (TableKey key in Enum.GetValues(typeof(TableKey)))
            dic[key] = row[key.GetName()].ToString(); // This line does not work!

        // adds dictionary to my list of dictionaries
        latestEntryList.Add(dic);
    }
}

I'm trying to replace this by using the forloop in the above code.

dic[TableKey.Barcode] = row["Barcode"].ToString();
dic[TableKey.FullName] = row["FullName"].ToString();
dic[TableKey.Location] = row["Location"].ToString();
dic[TableKey.Notes] = row["Notes"].ToString();
dic[TableKey.Category] = row["Category"].ToString();
dic[TableKey.Timestamp] = row["Timestamp"].ToString();
dic[TableKey.Description] = row["Description"].ToString();

EDIT: Maybe there is a way to combine the two foreach loops into one.

EDIT: I need to get the string name of the enum and the key value itself.

public enum TableKey
{
    Barcode = 0,
    FullName = 1,
    Location = 2,
    Notes = 3,
    Category = 4,
    Timestamp = 5,
    Description = 6
}

Solution

DataSet resultSet = Utils.RunQuery(sqlQuery);

if (resultSet.Tables.Count > 0)
{
    foreach (DataRow row in resultSet.Tables[0].Rows)
    {
         Dictionary<TableKey, string> dic = new Dictionary<TableKey, string>();
         foreach (TableKey key in Enum.GetValues(typeof(TableKey)))
            dic.Add(key, row[key.ToString()].ToString());

         latestEntryList.Add(dic);
    }
}
visc
  • 4,794
  • 6
  • 32
  • 58

3 Answers3

3
dic[Key] = row[key.ToString()].ToString();

Edit: I see a comment with this too. If that's made into answer, I'll delete this :)

AD.Net
  • 13,352
  • 2
  • 28
  • 47
3

Try the following:

       foreach (TableKey key in Enum.GetValues(typeof(TableKey)))
       {
            dic[key] = row[key.ToString("G")].ToString();
       }
Ananke
  • 1,250
  • 9
  • 11
  • what does this do: key.ToString("G")? – visc Oct 14 '14 at 15:12
  • 1
    It's just a format specifier for converting an enum to a string. The default specifier happens to be 'G' if none is specified. See: http://msdn.microsoft.com/en-us/library/c3s1ez6e(v=vs.110).aspx – Ananke Oct 14 '14 at 15:17
2

I think you can do it in one loop :

    // Makes the dictionary with populated keys from enum
    Dictionary<TableKey, string> dic = new Dictionary<TableKey, string>();
    foreach (TableKey key in Enum.GetValues(typeof(TableKey)))
        dic.Add(key, row[Enum.GetName(typeof(Direction), key)].ToString());

Edit: To get the enum 'value', you just cast it to int :

    // Makes the dictionary with populated keys from enum
    Dictionary<TableKey, string> dic = new Dictionary<TableKey, string>();
    foreach (TableKey key in Enum.GetValues(typeof(TableKey)))
        dic.Add(key, row[(int) key].ToString());
Orace
  • 7,822
  • 30
  • 45
  • You can even just delete the first loop since you can do `dic[somekeythatdoesntexistyet] = somevalue;` and it will `Add` it automatically. – Matt Burland Oct 14 '14 at 15:12
  • What makes this: `row[Enum.GetName(typeof(Direction), key)].ToString()` a better choice than `row[key.ToString()].ToString()` – visc Oct 14 '14 at 15:13
  • 1
    There is no good answer :o) http://stackoverflow.com/questions/483794/convert-enum-to-string but performance are better: Enum.GetName => 700 ms, ToString => 2000 ms – Orace Oct 14 '14 at 15:20