0

I have this code now and I would like to use a list of weekend instead of static days Sunday and Saturday . How can I use the equal function between an item z.DayOfWeekand a list of weekend.

    DateTime start = new DateTime(2021,3,3);
    DateTime end = new DateTime(2021,3,27);
    
    List<DateTime> blackOut = new List<DateTime>
    {
        new DateTime(2021,3,10),
        new DateTime(2021,3,11)
        
    };
    
var days = Enumerable.Range(0, (int)(end - start).TotalDays)
          .Select(x => start.AddDays(x))
          .Where(z => z.DayOfWeek != DayOfWeek.Saturday && 
                      z.DayOfWeek != DayOfWeek.Sunday &&
                      !blackOut.Contains(z))
          .Count();

Suppose that I have a list that contains weekend called weekendslist , what I have tried is :

List<string> weekendslist = new List<string>();
weekendslist.Add(DayOfWeek.Saturday.ToString());
weekendslist.Add(DayOfWeek.Sunday.ToString());
var days = Enumerable.Range(0, (int)(end - start).TotalDays)
          .Select(x => start.AddDays(x))
          .Where(z => weekendslist.Contains(z.DayOfWeek.ToString())
                      !blackOut.Contains(z))
          .Count();

But this does not work for me. Anyone have any idea how can I do to achieve this?

Ref : How to exclude blackout days from a specific duration between two dates chosen from a two Datepicker? C#

  • 1
    create a list of weekdays and then just call `Where(x => theList.Contains(x))`? What **specific** problem did you encounter here? – MakePeaceGreatAgain Mar 04 '21 at 13:35
  • Can you explain to me your approach please? –  Mar 04 '21 at 13:36
  • @HimBromBeere , the given days are not the same –  Mar 04 '21 at 13:37
  • The solution that you gave is the the same that I've tried –  Mar 04 '21 at 13:38
  • 1
    It seems that you forgot to negate your `weekendslist.Contains`. Currently you only get weekends. – Nikola Markovinović Mar 04 '21 at 13:44
  • @NikolaMarkovinović A big possibility, because I got 2 days now. –  Mar 04 '21 at 13:46
  • Could you please explain more your idea ? what should I change?? @NikolaMarkovinović –  Mar 04 '21 at 13:46
  • That's another problem , I get the days as an `Integer` and I create a list of string from these `Integer` are variables ( `DayId`) , so I have created a list of string , I have'nt any idea how can I create a list of `DayOfWeek` from `Integer` variables. –  Mar 04 '21 at 13:52
  • That's also not in your question. If you include what problem you're actually trying to solve in the question, it prevents people from wasting their time solving the problems you're not actually having... – Heretic Monkey Mar 04 '21 at 13:55
  • @TimSchmelter , the dayId is the Id of DayOfWeek ( from c#) , as an example Sunday will have `0` as dayId , Monday will have `1` –  Mar 04 '21 at 13:56
  • @HereticMonkey , I'm just trying to split my problem , I cannot put everything in one question , –  Mar 04 '21 at 13:57
  • How can I convert them ??? could you please how can I do this? –  Mar 04 '21 at 14:00
  • 1
    To follow on @TimSchmelter's comment, use the code in my answer, only instead of using a static list, use something like `myListOfInts.Select(myInt => (DayOfWeek)myInt)` – Heretic Monkey Mar 04 '21 at 14:01
  • 1
    See [How can I cast int to enum?](https://stackoverflow.com/q/29482/215552) – Heretic Monkey Mar 04 '21 at 14:02
  • Thanks , I solved it now –  Mar 04 '21 at 14:19

2 Answers2

0

How about using Enum.IsDefined?

enum Weekends
{
    Saturday,
    Sunday
}
var days = Enumerable.Range(0, (int)(end - start).TotalDays)
    .Select(x => start.AddDays(x))
    .Where(z => Enum.IsDefined(typeof(Weekends), z.DayOfWeek.ToString()) &&
                !blackOut.Contains(z))
    .Count();

Or another option would be to write an extension for DayOfWeek

public static class WeekendExtension
{
    public static bool IsWeekend(this DayOfWeek dayOfWeek)
    {
        return dayOfWeek == DayOfWeek.Saturday || 
            dayOfWeek == DayOfWeek.Sunday;
    }
}
var days = Enumerable.Range(0, (int)(end - start).TotalDays)
    .Select(x => start.AddDays(x))
    .Where(z => !z.DayOfWeek.IsWeekend() &&
                !blackOut.Contains(z))
    .Count();
Nate W
  • 275
  • 2
  • 13
  • Thank you for your answer, but the problem is that the Enum is something constant, however, the weekendslist can be changed every time . –  Mar 04 '21 at 13:45
  • How can I solve this error ?? `Extension method must be defined in a non-generic static class ` –  Mar 04 '21 at 13:48
  • If we're shooting at a shifting target, I need you to explain the question in a way that I can understand it. Your question, as worded, is talking about "excluding weekends," which implies the days you're hoping to exclude are constant. I don't understand the question. – Nate W Mar 04 '21 at 13:50
  • Put it into a static class. I edited my answer accordingly. – Nate W Mar 04 '21 at 13:51
  • My question is clear , I'm searching for a method to compare a list with an item using equals opertor in Where lambda function. –  Mar 04 '21 at 13:55
0

The following code is the same as your attempt, only using DayOfWeek enum values instead of strings, and properly negating the Contains:

DateTime start = new DateTime(2021, 3, 3);
DateTime end = new DateTime(2021, 3, 27);
List<DateTime> blackOut = new List<DateTime>
{
    new DateTime(2021, 3, 10), 
    new DateTime(2021, 3, 11)
};
List<DayOfWeek> weekendslist = new List<DayOfWeek>
{
    DayOfWeek.Saturday,
    DayOfWeek.Sunday
};
var days = Enumerable
    .Range(0, (int)(end - start).TotalDays)
    .Select(x => start.AddDays(x))
    .Where(z => 
           !weekendslist.Contains(z.DayOfWeek) && 
           !blackOut.Contains(z))
    .Count();
Console.WriteLine(days);
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122