开发者

DateTime and LinqToEntities

I have DateTime field, which storing date + time. I need to use only date part, so I try开发者_运维问答:

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

but I get the following error:

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

why and how to fix it?


what about this:

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


You cannot use extension functions in LINQ queries that result in a database hit if Entity Framework has no way to convert this into valid SQL.

There may be a more compact solution but the following should work fine:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜