1

I have enum:

public enum Days
{
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
    Sunday = 7
}

I use this enum to insert value as int as ID into database. But how can I "map" enum Days with that database IDs while I am retrieving value from database to show name of days instead of that database ID in my view?

For example I have a list of data shown, and currently I have DayId and ID shown, but how can I map this ID to show enum text (Monday, Tuesday,...) instead of ID(1,2,3..) ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
sensei
  • 7,044
  • 10
  • 57
  • 125
  • u also don't need to put numbers in front of every enum element. Just do it like `Monday = 1, Tuesday, ....` – shashwat Oct 15 '13 at 03:50
  • 5
    Do you know, that there's enum for this in framework: http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx – Giedrius Oct 15 '13 at 06:22
  • I know but there is a problem because the day starts with sunday for me and occupies 0 position, so I can't insert "select..." aka default value :) Dunno why. – sensei Oct 15 '13 at 15:07

5 Answers5

1

You don't really need anything special, you can cast an integer that you get from your database to your enum:

int valueFromDB = 4;
Days enumValue = (Days)valueFromDB;
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • Hi, this thread did the trick for me! https://stackoverflow.com/questions/1818131/convert-an-enum-to-another-type-of-enum – Whiplash Mar 22 '18 at 18:00
0

I would not suggest this approach. You need a look-up table for days. For instance

create table Days(
 DaysID INT PRIMARY KEY,
 Name VARCHAR(20))

All other tables will have a foreign key column of DaysID. The reason why I suggest against your approach is because your limiting yourself to hard coded values that may change.

If needed you can load your Days table into a List<KeyValuePair<int, string>>. If you leave as is, no one looking at the database will know ways DaysID 1, 2, 3, 4, etc represents.

I hope this helps.

chdev77
  • 505
  • 4
  • 18
0

Just you can use Enum.ToObject(typeof(Days), item) following extension method will help you.

private List<string> ConvertIntToString(Enum days, params int[]  daysIds)
{
    List<string> stringList = new List<string>();

    foreach (var item in daysIds)
    {
        stringList.Add(Enum.ToObject(days.GetType(), item).ToString());
    }

    return stringList;
}

use as follows:

ConvertIntToString(new Days(),2, 4, 6, 1);

or

        private List<Enum> ConvertIntToString(params int[] daysIds)
        {
            List<Enum> EnumList = new List<Enum>();
            foreach (var item in daysIds)
            {
                EnumList.Add((Days)item);
            }
            return EnumList;
        }
Thilina H
  • 5,754
  • 6
  • 26
  • 56
0

Use below extension method to wordify your enum

/// <summary>
/// Get the whilespace separated values of an enum
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static string ToEnumWordify(this Enum en)
{
    Type type = en.GetType();
    MemberInfo[] memInfo = type.GetMember(en.ToString());
    string pascalCaseString = memInfo[0].Name;
    Regex r = new Regex("(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])");
    return r.Replace(pascalCaseString, " ${x}");
}

Or you can supply Description to enum get it using below

public enum Manufacturer
{
    [Description("I did")]
    Idid = 1,
    [Description("Another company or person")]
    AnotherCompanyOrPerson = 2
}

/// <summary>
/// Get the enum description value
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static string ToEnumDescription(this Enum en) //ext method
{
    Type type = en.GetType();
    MemberInfo[] memInfo = type.GetMember(en.ToString());
    if (memInfo != null && memInfo.Length > 0)
    {
        object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attrs != null && attrs.Length > 0)
            return ((DescriptionAttribute)attrs[0]).Description;
    }
    return en.ToString();
}
Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
0

Please try following.

//Let's say you following ids from the database
 List<int> lstFromDB = new List<int>() { 1, 2, 3 };

 List<string> result = (from int l in lst
                        select ((Days)l).ToString()
                        ).ToList();
mit
  • 1,763
  • 4
  • 16
  • 27