0

I have this Enum

public enum Genders
    {
        [Description("Nữ")]
        Female,
        [Description("Nam")]
        Male
    }

I use this code to get each Enum name and value and save it to an Dictionary The result is

Female : 0

Male :1

    accountViewModel.Genders = Enum.GetValues(typeof(Account.Genders))
                               .Cast<Account.Genders>()
                               .ToDictionary(t => t.ToString(), t => (int)t);

How to mofify above code to get each Enum's Description and it value ?

Like this.

Nữ : 0

Nam : 1

user2877989
  • 587
  • 1
  • 6
  • 19

1 Answers1

1

According to an answer https://stackoverflow.com/a/2650090/643095,

string description = Enumerations.GetEnumDescription((MyEnum)value);

Where value is int enum value.

You can use something like this:

accountViewModel.Genders = Enum.GetValues(typeof(Account.Genders))
                               .Cast<Account.Genders>()
                               .ToDictionary(t => Enumerations.GetEnumDescription((Genders)t), t => (int)t);
Community
  • 1
  • 1
prvit
  • 956
  • 3
  • 9
  • 22