【问题标题】:Getting value and name from Enum in foreach loop在 foreach 循环中从枚举中获取值和名称
【发布时间】:2014-10-14 15:02:27
【问题描述】:

我有这本字典 Dictionary<TableKey, string>,其中 TableKey 是枚举类型。

我正在尝试使用我在 sql 查询期间获取的 DataSet 对象中的数据填充字典

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);
    }
}

我正在尝试使用上述代码中的 forloop 来替换它。

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();

编辑:也许有一种方法可以将两个 foreach 循环合并为一个。

编辑:我需要获取枚举的字符串名称和键值本身。

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

解决方案

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);
    }
}

【问题讨论】:

  • 您遇到了什么问题?
  • 我无法让 forloop 正常工作。我认为我使用了错误的方法。
  • 不应该只是:dic[key] = row[key.ToString()].ToString()
  • 当您说无法正常工作时,您的意思是什么?例外?还是错误的数据?
  • 不,我的意思是我需要正确设置它。我无法正确组合方法。

标签: c# dictionary enums


【解决方案1】:
dic[Key] = row[key.ToString()].ToString();

编辑:我也看到了这样的评论。如果这是答案,我会删除它:)

【讨论】:

    【解决方案2】:

    尝试以下方法:

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

    【讨论】:

    【解决方案3】:

    我认为你可以在一个循环中完成:

        // 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());
    

    编辑: 要获取枚举“值”,只需将其转换为 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());
    

    【讨论】:

    • 你甚至可以删除第一个循环,因为你可以这样做dic[somekeythatdoesntexistyet] = somevalue;,它会自动Add
    • 是什么让这个:row[Enum.GetName(typeof(Direction), key)].ToString()row[key.ToString()].ToString() 更好的选择
    • 没有好的答案 :o) stackoverflow.com/questions/483794/convert-enum-to-string 但性能更好:Enum.GetName => 700 ms, ToString => 2000 ms
    • 感谢您的关注!
    猜你喜欢
    • 2012-12-28
    • 2018-02-21
    • 1970-01-01
    • 2018-04-05
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多