0

I'm trying to convert an Enumeration to a Dictionary using LINQ

Here is what I have so far:

// 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 => "");
}

Its giving me issues when trying to cast the key.

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

I'm looking for this:

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

With a dictionary thats

["Barcode", ""]

I'm not sure what suits does here. I do want the string because I need to use it later in my program for comparisons.

visc
  • 4,794
  • 6
  • 32
  • 58
  • Why are you using `Enum.Parse` at all, if you only want strings? It would help if you'd explain what you want compared with the previous answer I gave you - an example of an enum along with the expected dictionary entries would be useful, for example. – Jon Skeet Oct 02 '14 at 16:40
  • So far you've said that you wrote some code and got some errors. What is your specific question? – Eric Lippert Oct 02 '14 at 16:42
  • How do I set this up correctly? I don't understand the types. – visc Oct 02 '14 at 17:03

1 Answers1

0

I think want you really want is Enum to List See: Convert an enum to List

But if you really need a Dictionary take a look at Enum to Dictionary c#

Community
  • 1
  • 1
Recep Karadas
  • 669
  • 4
  • 10