Oleg Sh
Oleg Sh

Reputation: 9001

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?

Upvotes: 2

Views: 173

Answers (2)

Chris Snowden
Chris Snowden

Reputation: 5002

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);

Upvotes: 1

Dean Chalk
Dean Chalk

Reputation: 20481

what about this:

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

Upvotes: 5

Related Questions