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);
    }
}
11
  • 1
    what the problem you are experiencing? Commented Oct 14, 2014 at 15:03
  • I can't get the forloop to work correctly. I think I'm using the wrong methods. Commented Oct 14, 2014 at 15:04
  • 2
    Shouldn't it just be: dic[key] = row[key.ToString()].ToString() Commented Oct 14, 2014 at 15:05
  • when you say not work correctly what do you mean there? An exfception? or wrong data? Commented Oct 14, 2014 at 15:05
  • No, I mean I need to set this up correctly. I can't get the method combination right. Commented Oct 14, 2014 at 15:07

3 Answers 3

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 :)

Sign up to request clarification or add additional context in comments.

Comments

3

Try the following:

       foreach (TableKey key in Enum.GetValues(typeof(TableKey)))
       {
            dic[key] = row[key.ToString("G")].ToString();
       }

2 Comments

what does this do: key.ToString("G")?
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: msdn.microsoft.com/en-us/library/c3s1ez6e(v=vs.110).aspx
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());

3 Comments

You can even just delete the first loop since you can do dic[somekeythatdoesntexistyet] = somevalue; and it will Add it automatically.
What makes this: row[Enum.GetName(typeof(Direction), key)].ToString() a better choice than row[key.ToString()].ToString()
There is no good answer :o) stackoverflow.com/questions/483794/convert-enum-to-string but performance are better: Enum.GetName => 700 ms, ToString => 2000 ms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.