枚举到词典LINQ

时间:2014-10-02 16:37:14

标签: c# linq dictionary enums

我尝试使用LINQ将枚举转换为字典

这是我到目前为止所做的:

// returns a dictionary populated with the relevant keys
public Dictionary<string, string> StartNewEntry()
{
    return Enum.GetNames(typeof(TableDictionary)).ToDictionary(Key => 
                         (string)Enum.Parse(typeof(TableDictionary), Key), value => "");
}

在尝试投射密钥时,它给了我一些问题。

Unable to cast object of type 'InventoryImportExportLibrary.TableDictionary' to type 'System.String'.

我正在寻找:

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

使用字典

[&#34; Barcode&#34;,&#34;&#34;]

我不确定这里适合做什么。我确实想要字符串,因为我需要在我的程序中稍后使用它进行比较。

1 个答案:

答案 0 :(得分:0)

我想你真的想要的是 枚举到列表请参阅:Convert an enum to List

但如果你真的需要一本词典,请看Enum to Dictionary c#