动态比较LINQ与SQL的时间 [英] Comparing times with LINQ to sql dynamically
问题描述
如果我在标题中正确描述,我想仅按小时和分钟值按日期时间值查询.
If I described right in the title, I want to query by datetime values just by hour and minute values.
事实上,我想按工作小时数来过滤当天的行.
In fact I want to filter rows on that day by hour of work or not.
我尝试在纯日期值中添加小时数
I tried it with adding hours to pure date value
elementsOturums.Where(i => i.EntryTime.Value.AddDays(1) < i.EntryTime.Value.Date.AddDays(1).AddHours(8).AddMinutes(30)
&& i.EntryTime.Value > i.EntryTime.Value.Date.AddHours(17).AddMinutes(15));
但是它给出了类似的错误;
but it gave error like;
数据类型为date的日期函数dateadd不支持datepart毫秒.
The datepart millisecond is not supported by date function dateadd for data type date.
然后我试图通过使用一种方法来克服,但是再次给出了类似的错误;
Then I tried to overcome by using a method but again it gave an error like;
方法'{0}'不支持SQL转换.
Method '{0}' has no supported translation to SQL.
那我该如何解决这个问题?
So how can I achieve this problem?
推荐答案
您应该按TimeSpans进行比较:
You should compare by TimeSpans:
var wdStart = TimeSpan.FromHours(8.5);
var wdEnd = TimeSpan.FromHours(17.25);
var result = elementsOturums.Where(t => t.EntryTime.HasValue)
.Where(t => t.EntryTime.Value.TimeOfDay >= wdStart
&& t.EntryTime.Value.TimeOfDay <= wdEnd);
这可以在工作时间内为您提供物品.
This gives you the items in working hours.
这篇关于动态比较LINQ与SQL的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!