locked
find if days are in sequence RRS feed

  • Question

  • User1786833724 posted

    Hi frineds,

    I have a function which is defined as follwing :

    public static List<DayOfWeek> ListDaysOfWeek(this MyClass obj)
    {
        // some stuff
    }

    When called, it does return the results as expected. But from where I am calling this C# function, I need to make the outcome more generic.

    For example :

    • "Weekdays" if ... my function returns : Mon to Fri ( abbreviating names the func does return correct enum values )
    • "Whole Week" if ... my function returns Sun, Mon, Tue, Wed, Thu, Fri, Sat
    • "Mondays" if ... it reuturns only Mon
    • "Mondays - Wednesdays" if ... it reuturns only Mon, Tue & Wed ( in sequence )
    • But let's consider the case when the function returns Mon, Wed, Fri ...

    I can not do the following :

    var days = ListDaysOfWeek(obj);
    
    message.AppendFormat("{0} - {1}", days.First(), days.Last());

    ... as it would print "Mondays - Fridays" which isn't correct.

    Is there any way to check of items in a List<DayOfWeek> are in sequnce ? I'd prefer LINQ solution if .NET does not have a direct way to do so.

    Tuesday, July 1, 2014 5:30 PM

All replies

  • User753101303 posted

    Hi,

    You could also create extension methods to provide various functions for a List<DayOfWeek>. Basically it seems you'll always create a list from a start day to an end day.

    You check it is in sequence but by desing it should have been created in sequence. I'm not sure how it relates to  the snippet you show.

    Ok so "it would print "Mondays - Fridays" which isn't correct" but what would you like to print instead ? My understanding is that you don't really need to check that this is in sequence for perhaps to output a string that would show all elements inside the sequence ??

    Tuesday, July 1, 2014 6:01 PM
  • User1786833724 posted

    Thanks for taking time, my apologies as it didn't make myself clear !

    PatriceSc

    Ok so "it would print "Mondays - Fridays" which isn't correct" but what would you like to print instead ? My understanding is that you don't really need to check that this is in sequence for perhaps to output a string that would show all elements inside the sequence ??

    I hear you Smile I can't just do the following ! Wink

    string.Join(", ", days.ToArray()); // ... or something like that

    Alright, let me rephrase, in the last bullet above, I am considering the case when let's say my function returned Monday, Wednesday and Friday. The way I have it now is as following :

    public static List<DayOfWeek> ListDaysOfWeek(this MyClass obj)
    {
        // some stuff
    }
    
    // and I call the function as following :
    
    var days = ListDaysOfWeek(obj); // days = { Mon, Wed, Fri }
    message.AppendFormat("{0} - {1}", days.First(), days.Last());

    This is a bug. Frown

    Basically In the scenario we are considering, the function ListDaysOfWeek has returned Mon, Wed & Fri NOT Mon, Tue, Wed, Thu, Fri. Even-though what gets appended to message is "Monday - Friday" - which is wrong in our case.

    I think if I can fix the problem, the output in this case should be "Monday, Wednesday, Friday" ...  Money Mouth

    Tuesday, July 1, 2014 11:37 PM