Using DateTime?.Value.TimeOfDay in LINQ Query

痞子三分冷 提交于 2019-11-28 07:24:13

问题


I'm trying to do a Query with LINQ on ASP.NET MVC 3.

I have a model, lets call it Event. This Event object has a Date property, of DateTime?. What I want is to fetch the Events that are between 2 TimeSpans.

Right now my code looks like the following:

TimeSpan From = new TimeSpan(8,0,0);
TimeSpan Until = new TimeSpan(22,0,0);

var events =
    from e in db.Events
    where e.Date.Value.TimeOfDay >= From,
          e.Date.Value.TimeOfDay <= Until
    select e;

An exception is thrown, telling me that "The specified type member 'TimeOfDay' is not supported in LINQ to Entities."

I don't get a way around this problem, and I have been all day trying. Please help me, I'm so frustrated. :(

EDIT:

I Forgot to write here the "TimeOfDay" after e.Date.Value. Anyway, I did in my code.

I can't use DateTime because I have to filter Events that occur between certain time of the day, despite the date of the event.


回答1:


Use the Date and Time Canonical Functions for LINQ-to-Entities. Specifically, look at

CreateTime(hour, minute, second)

If you need help calling a canonical function, look at How To: Call Canonical Functions.



来源:https://stackoverflow.com/questions/10000338/using-datetime-value-timeofday-in-linq-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!