是否可以给出枚举星期几的值

Is it possible to give enum dayofweek value

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

我试着给它赋值

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

但我收到错误

The switch statement contains multiple cases with the label value

我试过的代码:

public MyDayOfWeek Days { get; set; }
public string DaysFriendlyName => this.Days.ToFriendlyName();`
public enum MyDayOfWeek
{
    Sunday = DayOfWeek.Sunday,
    Monday = DayOfWeek.Monday,
    // ..

    SunTilFir = DayOfWeek.Sunday + DayOfWeek.Monday + DayOfWeek.Tuesday,//+...

    public static string ToFriendlyName(this MyDayOfWeek days)
    {
        switch (days)
        {
            case MyDayOfWeek.SunTilFir:
                    return @Resources.FormResources.SunTilFri;

是否可以设置这些值以及如何设置?提前致谢。

将 DayOfWeek 枚举包装在枚举中没有意义。我会创建您自己的助手 class 并添加满足检查范围所需的方法。当我需要知道一天是工作日还是周末等时,我使用了与下面类似的方法来处理支付系统……这似乎可行。

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
            }

希望能帮到您想出解决办法。