DateTime.Now in Where Clause

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
Posts: 16
Joined: Tue 23 Feb 2010 18:28

DateTime.Now in Where Clause

Post by lancelotti » Tue 11 May 2010 14:50

If I want to filter results by DateTime.Now it doesn't work.

Like this:

Code: Select all

var capa = from c in db.CapaAgendamentos where c.Publicacao <= DateTime.Now select c;
This is the SQL shown in debug mode... if I test directly in Oracle, it works.

Code: Select all

Directly in Oracle it returns only rows that "PUBLICACAO" was in the past hours, but in EntityFramework, it returns rows in the future too.

Any idea?

Ps. In the future I mean in the next hours, but not in the next days.

If I define a variable first, it works. Ex.:

Code: Select all

DateTime now = DateTime.Now;
var capa = from c in db.CapaAgendamentos where c.Publicacao <= now select c;

Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Wed 12 May 2010 15:59

The reason for this situation is the fact that when you are passing DateTime.Now directly to the query, the value for it is calculated by the server taking into account the specific time zone and some other session parameters.
And in case you are passing variable, it is treated as a parameter and the value of this parameter is set exactly the same as the one you set in code.

Post Reply