0

I am trying to write a LINQ or LINQPad4 Query to check if a DateRange falls over a weekend.

Does anybody have a method of doing this?

It could either check if any of the named days within the DateRange is "Sat" if that is an easier way to do it.

If it is not possible to do this for a DateRange is there any way to check the named day of a specific DateTime?

==EDIT== I am trying to do this all within the LINQ Query Expression to Query an SQL Datebase.

Joe_DM
  • 985
  • 1
  • 5
  • 12

2 Answers2

2
Console.WriteLine(dateValue.ToString("ddd"));    // Displays Wed

A simple between check is quite easy:

var isInRange = dateToCheck >= startDate && dateToCheck < endDate;

As @Sergey Berezovskiy answered here:

You could also use Martin Fowlers date range:

public interface IRange<T>
{
    T Start { get; }
    T End { get; }
    bool Includes(T value);
    bool Includes(IRange<T> range);
}

public class DateRange : IRange<DateTime>         
{
    public DateRange(DateTime start, DateTime end)
    {
        Start = start;
        End = end;
    }

    public DateTime Start { get; private set; }
    public DateTime End { get; private set; }

    public bool Includes(DateTime value)
    {
        return (Start <= value) && (value <= End);
    }

    public bool Includes(IRange<DateTime> range)
    {
        return (Start <= range.Start) && (range.End <= End);
    }
}

Usage:

DateRange range = new DateRange(startDate, endDate);
range.Includes(date)
Community
  • 1
  • 1
crthompson
  • 15,653
  • 6
  • 58
  • 80
  • Hi, This would be easy if I was doing it in `VS` however I am trying to write it into the `LINQ` Query. You cannot use a .ToString within the `LINQ` Query. – Joe_DM Jan 18 '14 at 07:02
  • 1
    @paqogomez thanks, +1 thus OP didn't mentioned that he is querying database, it's nice solution – Sergey Berezovskiy Jan 18 '14 at 07:07
  • @paqogomez Thanks for the advise and sorry I wasn't more clear, I though LINQ Query Expression was for Querying Databases. I have added details to the question for further clarification. – Joe_DM Jan 18 '14 at 07:11
2

With Linq to SQL you can use properties of DateTime. You can introduce new range variable which will hold current day of week, and then use it to check whether day falls in weekend:

from f in Foos
let day = f.Date.DayOfWeek
where day == DayOfWeek.Saturday || day == DayOfWeek.Sunday
select f
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thanks, Is there any way to do this check on a `DateRange` as a part of the Select statement? – Joe_DM Jan 18 '14 at 07:22
  • @Joe_DM yes, you can pass fromDate and toDate, and check whether `fromDate >= f.Date && f.Date <= toDate` – Sergey Berezovskiy Jan 18 '14 at 07:24
  • Just a note, when trying this it seems you need to apply the .DayOfWeek on a .Value e.g. let day = so.DateReceived.Value.DayOfWeek – Joe_DM Jan 18 '14 at 07:26
  • @Joe_DM that's because you have `Nullable` instead of simple `DateTime` type. Sorry, need to go, will be back in hour – Sergey Berezovskiy Jan 18 '14 at 07:27
  • 1
    This is working great by the way, Thanks. I am finding the name of the first day and then the amount of days in the `DateRange` which gives me logic to work with. – Joe_DM Jan 20 '14 at 04:25