通过DayOfWeek枚举的每个循环在星期一开始?

时间:2013-06-28 13:56:23

标签: c# enums dayofweek

我正在迭代DayOfWeek Enum:

foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)))
{
 // Add stuff to a list 
}

我的问题是,我希望我的枚举能够在星期一而不是星期天开始。

我试着这样做:

CultureInfo ci = Thread.CurrentThread.CurrentCulture;
ci.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;

但是foreach循环仍然在周日开始。

我该怎么做?

我的最后一个想法是将我的结果列表重新排序为我想要的日期顺序 但这意味着更多的迭代。

谢谢!

6 个答案:

答案 0 :(得分:20)

这是不可能的,纯粹是因为设置文化不会改变DayOfWeek enum被定义为这样的事实:

public enum DayOfWeek {
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
}

但是,您可以跳过第一个条目并稍后添加..也许是这样:

foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek))
                              .OfType<DayOfWeek>()
                              .ToList()
                              .Skip(1)) {
    list.Add(day.ToString());
}
list.Add(DayOfWeek.Sunday.ToString());

答案 1 :(得分:8)

OrderBy的单次调用可以根据需要对其进行排序。无法将呼叫顺序更改为Enum.GetValues

var daysOfWeek = Enum.GetValues(typeof(DayOfWeek))
    .OfType<DayOfWeek>()
    .OrderBy(day => day < DayOfWeek.Monday);

答案 2 :(得分:2)

使用 for 循环和modulo(%)运算符:

DayOfWeek day;
for( int i = 0 ; i < 7 ; i++ )
{
    day = (DayOfWeek) ((i + 1) % 7);
    // Add stuff to a list
}

如果你想使用这个循环移动另一个枚举,你总是可以用这样初始化的变量替换数字7:

int length = Enum.GetValues(typeof(DayOfWeek)).Length;

你甚至可以编写一个移动任何枚举的方法,假设这个枚举具有 0,1,...,n 之间的所有值:

static IEnumerable<TEnum> Shift<TEnum>(int offset) where TEnum : struct
{
    int length = Enum.GetValues(typeof(TEnum)).Length;
    for( int i = 0 ; i < length ; i++ )
    {
        yield return (TEnum) (object) ((i + offset) % length);
    }
}

答案 3 :(得分:1)

从自定义IComparer<DayOfWeek>开始:

public class DayOfWeekComparer : IComparer<DayOfWeek> {
    public int Compare(DayOfWeek x, DayOfWeek y) {
        return ModifyDayOfWeek(x).CompareTo(ModifyDayOfWeek(y));
    }

    private static int ModifyDayOfWeek(DayOfWeek x) {
        // redefine Sunday so it appears at the end of the ordering
        return x == DayOfWeek.Sunday ? 7 : (int)x;
    }
}

然后:

foreach(DayOfWeek day in Enum.GetValues(typeof(DayOfWeek))
                             .OfType<DayOfWeek>()
                             .OrderBy(x => x, new DayOfWeekComparer())) {
    // will see Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

编辑添加:

实际上,即使 太复杂了,也就像它一样漂亮。你为什么不把它放在

static IEnumerable<DayOfWeek> DaysOfWeek {
    get {
        yield return DayOfWeek.Monday;
        yield return DayOfWeek.Tuesday;
        yield return DayOfWeek.Wednesday;
        yield return DayOfWeek.Thursday;
        yield return DayOfWeek.Friday;
        yield return DayOfWeek.Saturday;
        yield return DayOfWeek.Sunday;
   }
}

某个地方,任何地方,只是完成它! KISS

答案 4 :(得分:0)

Enum.GetValues返回按枚举常量的二进制值排序的元素(请参阅MSDN上的备注)。以这种方式定义DayOfWeek枚举:

public enum DayOfWeek
{
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
}

如您所见,Monday定义为1Sunday定义为0。因此,您无法强制Enum.GetValues方法在Monday之前返回Sunday

答案 5 :(得分:0)

为我工作:

class _MyAppState extends State<MyApp> {
  GlobalKey qrKey = GlobalKey();
  var qrText = "";
  QRViewController controller;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Column(
        children: [
          Expanded(
              flex: 5,
              child: QRView(
                key: qrKey,
                overlay: QrScannerOverlayShape(
                    borderRadius: 10,
                    borderColor: Colors.red,
                    borderLength: 30,
                    borderWidth: 10,
                    cutOutSize: 300),
                onQRViewCreated: _onQRViewCreate,
              )),
          Expanded(
            flex: 1,
            child: Center(
              child: InkWell(
                child: Text(
                  'Scan result: $qrText',
                ),
                onTap: () {
                  if (qrText.isNotEmpty) {
                    launch(qrText);
                  }
                },
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  void _onQRViewCreate(QRViewController controller) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
      setState(() {
        qrText = scanData;
      });
    });
  }
}