在LINQ查询使用日期时间?.Value.TimeOfDay [英] Using DateTime?.Value.TimeOfDay in LINQ Query

查看:557
本文介绍了在LINQ查询使用日期时间?.Value.TimeOfDay的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个LINQ查询的ASP.NET MVC 3。

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

我有一个模型,让我们把它叫做事件。此事件对象有一个日期属性日期时间,?我想要的是获取这2个时间跨度之间的活动。

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.

现在我的code如下所示:

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;

这是抛出异常,告诉我,指定的类型成员的TimeOfDay在LINQ是不支持的实体。

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. :(

编辑:

我忘了写在这里e.Date.Value后的TimeOfDay。无论如何,我做我的code。

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.

推荐答案

使用的对LINQ到实体的日期和时间规范函数。详细的,请看

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.

这篇关于在LINQ查询使用日期时间?.Value.TimeOfDay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆