是否可以给予enum dayofweek值

时间:2017-01-30 11:55:34

标签: c# asp.net-mvc enums dayofweek

我有一个枚举值,表示我以友好名称呈现给用户的工作日范围(例如星期日到星期五)。

我试着给它赋值

created_date

但是我收到了错误

  

switch语句包含标签值为

的多个案例

我试过的代码:

(SunTilTHur = DayOfWeek.Sunday + DayOfWeek.Monday + DayOfWeek.Tuesday)

是否可以设置此值以及如何设置?提前谢谢。

1 个答案:

答案 0 :(得分:2)

将DayOfWeek枚举包装在枚举中是没有意义的。我会创建自己的帮助器类并添加满足检查范围所需的方法。当我需要知道支付系统的某一天是工作日或周末等时,我已经使用了类似的方法,这似乎有效。

public static class MyDayHelper
{
    public static bool IsWeekDay(DayOfWeek myday)
    {
        return myday >= DayOfWeek.Monday && myday <= DayOfWeek.Friday;
    }

    public static bool IsWeekEnd(DayOfWeek myday)
    {
        return myday == DayOfWeek.Saturday || myday == DayOfWeek.Sunday;
    }

    public static string ReturnWeekdays()
    {
        return string.Format("{0},{1},{2},{3},{4}", DayOfWeek.Monday, DayOfWeek.Tuesday,
            DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday);
    }

    public static string SunTilFri
    {
        get
        {
            return string.Format("{0},{1},{2},{3},{4},{5}", DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
                DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday);
        }
    }

}

您显然可以为您添加相关方法......然后您可以根据当天的情况测试条件

var myDay = DateTime.Now.DayOfWeek;

            if (MyDayHelper.IsWeekDay(myDay))
            {
                //Do something
            }

            if (MyDayHelper.IsWeekEnd(myDay))
            {
                //Do something
            }

希望能帮助您找到解决方案。