Question

I am iterating through the DayOfWeek Enum like this :

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

And my problem is that I would like my enum to start by Monday instead of Sunday.

I tried to do :

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

But the foreach loop still started with Sunday.

How can I do this ?

My last idea would be to reorder my resulting list to the order of day I want but that would mean more iterations.

Thanks !

Was it helpful?

Solution

That isn't possible, purely because setting the culture doesn't change the fact that the DayOfWeek enum is defined as such:

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

You can, however, skip the first entry and add it later.. perhaps like this:

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

OTHER TIPS

A single call to OrderBy can order them as desired. It's not possible to change the order of the call to Enum.GetValues.

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

Use a for loop and the modulo (%) operator :

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

If you want to use this loop to shift another enum, you can always replace the number 7 with a variable initialized like this :

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

You could even write a method to shift any enum, assuming that this enum has all the values between 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);
    }
}

Start with a custom 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;
    }
}

Then:

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

Edit to add:

Actually, even that is too complicated, as pretty as it is. Why don't you just put

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

somewhere, anywhere, and just be done with it! KISS!

Enum.GetValues returns elements which are sorted by the binary values of the enumeration constants (see Remarks on MSDN). DayOfWeek enumeration defined this way:

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

As you can see, Monday defined as 1 and Sunday defined as 0. So, you can't force Enum.GetValues method to return Monday before Sunday.

This work for me:

foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek))
                      .OfType<DayOfWeek>()
                      .ToList()
                      .Skip(1).Union(new List<DayOfWeek> { DayOfWeek.Sunday}))
{
    // Do stuff
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top