Monochromie
Monochromie

Reputation: 449

LINQ 2 Entities-query is not working, but why?

everyone! )) Here is code.

var existingEntities = (from record in globalOne.serviceContext.records_out
                        where record.timestamp.Date == DateTime.Now.Date
                        select record ).ToList();

It doesn't work.

Another code:

var existingEntities = (from record in globalOne.serviceContext.records_out
                 where record.timestamp.Day == DateTime.Now.Day
                 select record ).ToList();

It does work.

So, problem id in next string:

where record.timestamp.**Date** == DateTime.Now.Date

also won't do

where record.timestamp.Date.Equals(DateTime.Now.Date)

But why? I have no clue. "Timestamp" field is dateTime field in MS SQL SERVER. And - there is NO records in table.

And I almost forgot - what does it mean - "doesn't work". App just will not reach the breakpoint after that query(first), without any error, without anything.

Thanks.

Upvotes: 2

Views: 835

Answers (1)

Saeed Amiri
Saeed Amiri

Reputation: 22565

You can call record.timestamp.Date because EF can't convert it to required expression tree (then convert it to sql command). In fact EF supports limited number of functions and properties, But for DateTime, EF has some good Canonical functions. You can use them in your case, e.g you can use Day(),Month(),Year() functions to solve your problem (see the link).

Upvotes: 2

Related Questions