Issue with DateTime.Now in where clause

Posts   
 
    
wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 28-May-2008 17:05:26   

Hi,

with the latest LLBLGen (20th may) this query doesn't seem to work, as it doesn't return any entities:

var elementEntities = from element in _data.Element
                                  where element.PublicationDate <= DateTime.Now 
                                  && element.ArchiveDate == null
                                  select element;

However this works:

            var elementEntities = from element in _data.Element
                                  where element.PublicationDate <= DateTime.Now.AddTicks( 1 ) 
                                    && element.ArchiveDate == null
                                  select element;

In the first situation, no date at all is passed to the SQL

(   Query: SELECT DISTINCT TOP(@top0) COUNT(*) AS [LPAV_] FROM [TEST_CC_ORM].[dbo].[gtb_Element] [LPLA_1]  WHERE ( ( ( ( ( [LPLA_1].[__pub_date] IS NULL) AND ( [LPLA_1].[__arch_date] IS NULL)))))
    Parameter: @top0 : Int64. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1 )

but in the second there is:

    Query: SELECT DISTINCT TOP(@top0) COUNT(*) AS [LPAV_] FROM [TEST_CC_ORM].[dbo].[gtb_Element] [LPLA_1]  WHERE ( ( ( ( ( [LPLA_1].[__pub_date] <= @PublicationDate2) AND ( [LPLA_1].[__arch_date] IS NULL)))))
    Parameter: @top0 : Int64. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.
    Parameter: @PublicationDate2 : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 28-5-2008 16:55:45.

Seems like DateTime.Now is recognized as an expression but doesn't get translated to something like GETDATE()?

Best Regards,

Wiebe

wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 28-May-2008 17:59:31   

It seems to be more related to comparing a DateTime? to a DateTime value instead of DateTime.Now, because this does seem to work:


var elementEntities = from element in _data.Element
                                 where element.PublicationDate.Value <= DateTime.Now
                                 && element.ArchiveDate == null
                                 select element
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39872
Joined: 17-Aug-2003
# Posted on: 28-May-2008 18:03:19   

It could be the nullable type (a fix for that will be uploaded in the new build in a couple of minutes wink ) but DateTime.Now isn't mapped to a SQL equivalent.

This is explained in the new documentation in the new build. It comes down to: what is meant with DateTime.Now ? The query is specified on the CLIENT, so is the datetime on the client the value to use? Or is it the datetime on the SERVER ?

THis can make a big difference so we didn't add a mapping for this. It's easy to add a mapping for this however. In the new docs (in the new build, available in a couple of minutes) this is explained in depth.

Frans Bouma | Lead developer LLBLGen Pro