2011-08-15 120 views
2

我有DateTime字段,其中存儲日期+時間。我只需要使用日期部分,所以我嘗試:DateTime和LinqToEntities

  query = query.Where(p => p.CreatedDateTime.Date == DateStart); 

,但我得到了以下錯誤:

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

爲什麼和如何解決?

回答

5
這個

什麼:

query = query.Where(p => EntityFunctions.TruncateTime(p.CreatedDateTime) == DateStart); 
1

您不能導致命中如果Entity Framework有沒有辦法將其轉換成有效的SQL數據庫LINQ查詢使用擴展功能。

有可能是一個更緊湊的解決方案,但以下罰款應該工作:

query = query.Where(p => 
    p.CreatedDateTime.Year == DateStart.Year && 
    p.CreatedDateTime.Month == DateStart.Month && 
    p.CreatedDateTime.Day == DateStart.Day);