简体   繁体   English

将DateTime.Now与SQL Server的开始时间和结束时间进行比较

[英]Comparing DateTime.Now to a Start and End time from SQL Server

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 ). 我有一个SQL Server表,其中包含列DayOfWeekDateTime ), StartTimeTime ), EndTimeTime )。

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 . 我正在尝试编写一个lambda select,以获取DateTime.Now是相同的DayOfWeek所有行,大于StartTime且小于EndTime

I think I have to use the SQL Server type Time as C# Timespan but I'm just not getting it right. 我想我必须使用SQL Server类型的Time作为C# Timespan但我只是Timespan不好。

I've tried to work from the explanation here ( Datetime.now as TimeSpan value? ) but to no avail. 我尝试根据此处的说明进行工作( Datetime.now作为TimeSpan值? ),但无济于事。

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. 任何帮助,不胜感激。

I don't know how I missed it, DateTime.Now.TimeOfDay is what I needed. 我不知道我怎么想念它,我需要DateTime.Now.TimeOfDay。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM