0

I've been struggling with this for hours and am sure there is a simple explanation.

I have a SQL Server table that contains the columns DayOfWeek (DateTime), StartTime (Time), EndTime (Time).

I am trying to write a lambda select that gets all the rows where DateTime.Now is the same DayOfWeek, greater than StartTime and less than EndTime.

I think I have to use the SQL Server type Time as C# Timespan but I'm just not getting it right.

I've tried to work from the explanation here (Datetime.now as TimeSpan value?) but to no avail.

My line of code is:

context.DayAndTimeModifiers
       .Where(x => x.IsActive && (int)d.DayOfWeek == x.DayOfWeek 
                   && d.??? > x.StartTime 
                   && d.??? < x.EndTime)

Any help much appreciated.

Community
  • 1
  • 1

1 Answers1

2

I don't know how I missed it, DateTime.Now.TimeOfDay is what I needed. Here's the adjusted line of code...

var d = DateTime.Now;

return context.DayAndTimeModifiers
              .Where(x => x.IsActive && (int)d.DayOfWeek == x.DayOfWeek && 
                          d.TimeOfDay > x.StartTime && d.TimeOfDay < x.EndTime).ToList();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459