简体   繁体   English

实体框架-where子句中的DateTime

[英]Entity Framework - DateTime in the where clause

I'm trying to convert this code from a stored procedure into a "LINQ to Entities" query: 我正在尝试将此代码从存储过程转换为“ LINQ to Entities”查询:

select * from myTable
where venue_date + start_time <= @EndDate
and dateadd(minute, duration * 24 * 60, venue_date + start_time) >= @StartDate

As you can see there is some manipulation of dates. 如您所见,有一些日期操作。 This sort of thing won't work because functions such as .AddHours() are not supported: 由于不支持诸如.AddHours()类的功能,因此这种事情不起作用:

where b.Venue_Date.Value.AddHours(b.Start_Time.Value.Hour) <= request.EndDate

Something like this won't work either. 这样的事情也不起作用。 The error says something about not supporting constructors with parameters: 该错误说明了有关不支持带参数的构造函数的信息:

where new DateTime(b.Venue_Date.Value.Year, b.Venue_Date.Value.Month,
  b.Venue_Date.Value.Day, b.Start_Time.Value.Hour, b.Start_Time.Value.Minute,
  b.Start_Time.Value.Second) <= request.EndDate

It seems that the Entity Framework is VERY restrictive when dealing with dates. 似乎在处理日期时,实体框架非常严格。 I guess I'll have to use a procedure. 我想我必须使用一个程序。 The next problem is figuring out how to make a procedure that can populate several related object collections. 下一个问题是弄清楚如何制作一个可以填充几个相关对象集合的过程。

Is changing the schema an option? 更改架构是一种选择吗? My experience is that when storing separate columns like: 我的经验是,当存储单独的列时,例如:

  • Date 日期
  • Start Time (as a date) 开始时间(日期)
  • Stop time (as a date) 停止时间(日期)

...separately, it's a huge advantage to store the date redundantly in the Date and StartTime columns. ...另外,将日期冗余存储在DateStartTime列中是一个巨大的优势。 Not the least because it makes your query (1) easier to write, and (2) the query can now use an index. 尤其重要的是,它使您的查询(1)易于编写,并且(2)查询现在可以使用索引。

I've done this both ways, and I much prefer this method. 我已经完成了这两种方式,并且我更喜欢这种方法。

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

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