2

I have a column of type int called SelectedDays in my database. I decided to go with the bit approach:

sun=1, mon=2, tue=4, wed=8, thu=16, fri=32, sat=64

If the user selects Sun, Mon, Wed then the value in the field would be 11

In my C# model I am trying to make a readonly property:

public List<string> Days

that will return a list of the selected days in string format.

I can't figure out how to go from 11 to "Sunday", "Monday", "Wednesday"

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • 3
    This should do the trick: http://stackoverflow.com/questions/10112076/list-all-bit-names-from-a-flag-enum – mbrdev Dec 09 '16 at 16:32

1 Answers1

2
int valueFromDb = 11;
var result = Enum.GetNames(typeof(WeekDays))
                 .Where(x => (valueFromDb & (int)Enum.Parse(typeof(WeekDays), x)) > 0)
                 .ToList();

You can do it like this. Full example

mybirthname
  • 17,949
  • 3
  • 31
  • 55