Page 1 of 1

DateTime.Now in Where Clause

Posted: Tue 11 May 2010 14:50
by lancelotti
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

SELECT * FROM SITE.TD_CAPA_AGENDAMENTO WHERE PUBLICACAO <= (CURRENT_DATE) 
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.

[UPDATE]
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;

Posted: Wed 12 May 2010 15:59
by AndreyR
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.