在LINQ to Entity Framework查询中忽略日期时间的时间部分

问题描述:

如何在不忽略datetime列的时间部分的情况下执行LINQ to Entity Framework查询?当使用LINQ TO EF时,不支持使用MyTable.MyColumn.Date之类的东西(认为它可以与LINQ to SQL一起使用),据我所知,您不能像在普通SQL中那样使用Convert/cast(或者可以?)

我需要在where语句中尝试执行类似"... where mytable.datecolumn =(select min(datecolumn)...)"的操作.但是忽略时间部分(想要选择日期与子查询返回的日期相同的所有行).

How can I do a LINQ to Entity Framework query where you ignore the time portion of a datetime column? Using something like MyTable.MyColumn.Date is not supported when using LINQ TO EF (think it works with LINQ to SQL) and as far as I know you can't use a Convert/cast like you can in normal SQL (or can you?) 

I need this in the where statement trying to do something like "... where mytable.datecolumn = (select min(datecolumn) ...) " but ignoring timepart (want to select all rows where date is same as the date returned from the subquery).

您可以将其视为常规DateTime,但它可以将其视为常规DateTime是否可以为空(即DateTime?).

例如:

You can treat it like a regular DateTime except that it is nullable (i.e. DateTime?).

For instance:

var x = myContext .MyTable.Where( i = > i. MyDateColumn.GetValueOrDefault().Date
...);
var x = myContext.MyTable.Where(i => i.MyDateColumn.GetValueOrDefault().Date   
... );