Convert integer to a list of week days

后端 未结 4 1346
借酒劲吻你
借酒劲吻你 2021-01-21 13:45

I have an integer stored in a database (SQLAgent Frequency Interval) This integer is actually the sum of the selected days of the week that the schedule is to run The possible v

4条回答
  •  孤独总比滥情好
    2021-01-21 14:42

    Edit: This is C# code to do the bit operations. I posted it before reading the question in detail, but I will leave it here as an alternative. Is the database really the best place to do this...?

    You can use an array:

    // input: int value
    string[] weekdays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday" };
    int flag = 1
    List days = new List();
    foreach (string day in days) {
       if ((value && flag) != 0) {
          days.Add(day);
       }
       flag <<= 1;
    }
    

    The result is a list of strings, if you want to merge them you can for example do:

    string selected = String.Join(", ", days.ToArray());
    

提交回复
热议问题