改变这个序列的最优雅的方式

发布于 2024-08-28 19:43:04 字数 567 浏览 8 评论 0原文

我已将一周中的某一天存储在数据库表中(我无法控制),并且我需要在代码中使用它。

问题是,我想使用 System.DayOfWeek 枚举来表示这一点,并且序列不相同。

在数据库中,如下:

1  2  3  4  5  6  7
S  M  T  W  T  F  S

我需要如下:

0  1  2  3  4  5  6
M  T  W  T  F  S  S

执行此操作最优雅的方法是什么?

例如,我可以这样做:

i = dayOfWeek;
i = i - 2;
if (i < 0) {
    i = 6;
}

但这有点不优雅。有什么建议吗?

<编辑>

咳咳。显然(.net 反射器说)DayOfWeek 的索引为 0,从星期日开始。

在问愚蠢的问题之前一定要先阅读文档。

不过,我还是对答案感兴趣,只是为了满足自己的好奇心,所以就去吧。

I've got the Day of the week stored in a database table (that I do not control), and I need to use it in my code.

Problem is, I want to use the System.DayOfWeek enum for representation for this, and the sequences are not the same.

In the database, it's as follows:

1  2  3  4  5  6  7
S  M  T  W  T  F  S

I need it as follows:

0  1  2  3  4  5  6
M  T  W  T  F  S  S

What's the most elegant way to do this?

for example, I could do:

i = dayOfWeek;
i = i - 2;
if (i < 0) {
    i = 6;
}

but that's a bit inelegant. Any suggestions?

<EDIT>

Ahem. Apparently (.net reflector says) DayOfWeek is 0 indexed starting with Sunday.

Always read the docs before asking daft questions.

However, I'm still interested in an answer, just to satisfy my own curiosity, so go for it.

</EDIT>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囚你心 2024-09-04 19:43:04

您想要的值

(DayOfWeek)((dbDay + 5) % 7)

使用模运算符 %

The value you want is

(DayOfWeek)((dbDay + 5) % 7)

using the modulo operator %.

℉絮湮 2024-09-04 19:43:04

将其包装在一个函数中:

public int DbToDayOfWeek(int dbDay)
{
   if (dbDay == 1)
     return 6;

   return dbDay  -2;

}

或者:

public DayOfWeek DbToDayOfWeek(int dbDay)
{
   if (dbDay == 1)
     return DayOfWeek.Sunday;

   return (DayOfWeek)(dbDay - 2);

}

Wrap it in a function:

public int DbToDayOfWeek(int dbDay)
{
   if (dbDay == 1)
     return 6;

   return dbDay  -2;

}

Or:

public DayOfWeek DbToDayOfWeek(int dbDay)
{
   if (dbDay == 1)
     return DayOfWeek.Sunday;

   return (DayOfWeek)(dbDay - 2);

}
恍梦境° 2024-09-04 19:43:04

尽管我无法想象这些值会发生变化,但您确实应该避免假设 DayOfWeek 枚举值将保持不变 - 因此进行相应的编码。

static DayOfWeek[] _toDaysTable = new DayOfWeek[] {
    DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday,
    DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday
};

static DayOfWeek ToDayOfWeek(int myDayOfWeek)
{
     int index = myDayOfWeek - 1;
     if (index < 0 || index >= _toDaysTable.Length)
          throw new ArgumentOutOfRangeException("myDayOfWeek");
     return _toDaysTable[index];
}

static int FromDayOfWeek(DayOfWeek day)
{
    int index = Array.IndexOf(_toDaysTable, day);
    if (index < 0)
        throw new ArgumentOutOfRangeException("day");
    return index + 1;
}

Although I can't imagine the values changing, you should really avoid assuming that the DayOfWeek enumerated values will stay the same - so code accordingly.

static DayOfWeek[] _toDaysTable = new DayOfWeek[] {
    DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday,
    DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday
};

static DayOfWeek ToDayOfWeek(int myDayOfWeek)
{
     int index = myDayOfWeek - 1;
     if (index < 0 || index >= _toDaysTable.Length)
          throw new ArgumentOutOfRangeException("myDayOfWeek");
     return _toDaysTable[index];
}

static int FromDayOfWeek(DayOfWeek day)
{
    int index = Array.IndexOf(_toDaysTable, day);
    if (index < 0)
        throw new ArgumentOutOfRangeException("day");
    return index + 1;
}
花开半夏魅人心 2024-09-04 19:43:04

您可以创建自己的枚举并将枚举直接映射到数据库中的值

public enum DayOfWeek
{
    Mon = 2,
    Tue = 3,
    Wed = 4,
    Thu = 5,
    Fri = 6,
    Sat = 7,
    Sun = 1
}

然后您可以使用 DayOfWeek 类型的扩展方法来检索值:

public static int ToInt(this DayOfWeek dow)
{
    return (int)dow;   
}

除非您依赖于 DayOfWeek 用于与日期进行实际比较,否则您将必须在偏移量之间进行转换。

You could just create your own enum and map the enum directly to the value in the database

public enum DayOfWeek
{
    Mon = 2,
    Tue = 3,
    Wed = 4,
    Thu = 5,
    Fri = 6,
    Sat = 7,
    Sun = 1
}

Then you could use an extension method of your DayOfWeek type to retrieve the value:

public static int ToInt(this DayOfWeek dow)
{
    return (int)dow;   
}

Unless you are relying on the DayOfWeek for actual comparisons with Dates, otherwise you will have to do the conversion between the offsets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文