简体   繁体   English

修改此序列的最优雅方法

[英]Most elegant way to morph this sequence

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. 问题是,我想使用System.DayOfWeek枚举对此进行表示,并且序列不相同。

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. 显然(.net反射器说)从星期日开始,DayOfWeek的索引为0。

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> </ EDIT>

The value you want is 您想要的价值是

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

using the modulo operator % . 使用模运算符%

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);

}

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. 尽管我无法想象这些值会发生变化,但是您应该真正避免假设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;
}

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: 然后,您可以使用DayOfWeek类型的扩展方法来检索值:

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. 除非您依赖DayOfWeek与Date进行实际比较,否则必须在偏移量之间进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM