6

Are DateTime functions in an EF query evaluated by the SQL Server, as where DateTime functions outside of the query expression are evaluated by the machine running the IL?

I have an application that has SalesOrders

public class SalesOrder
{
   public Int32 OrderID {get;set;}
   public DateTime Expiration {get;set;} 
}

I run an EF query and get different results when I do this:

DateTime utcnow = DateTime.UtcNow;

var open = (from a in context.SalesOrders
            where a.Expiration > utcnow
            select a).ToList();

Than when I do this:

var open = (from a in context.SalesOrders
            where a.Expiration > DateTime.UtcNow
            select a).ToList();

I think this is because DateTime.UtcNow in an Entity Framework query is evaluated by the SQL Server, vs DateTime.UtcNow outside of the query is evaluated by the machine that's running the IL; I'm basing that off this answer.

I'm in Azure platform as a service, debugging locally with an Azure SQL DB, if that matters.

Community
  • 1
  • 1
Eric
  • 2,273
  • 2
  • 29
  • 44

3 Answers3

8

Your thoughts are correct.

On SQL Server, your first query runs the following SQL query:

exec sp_executesql N'SELECT 
    [Extent1].[OrderID] AS [OrderID], 
    [Extent1].[Expiration] AS [Expiration]
    FROM [dbo].[SalesOrders] AS [Extent1]
    WHERE [Extent1].[Expiration] > @p__linq__0',N'@p__linq__0 datetime2(7)',@p__linq__0='2016-01-08 20:05:25.4433282'

It is clear here that the client's time is passed in as a parameter.

Your second query sends this to SQL server:

SELECT 
    [Extent1].[OrderID] AS [OrderID], 
    [Extent1].[Expiration] AS [Expiration]
    FROM [dbo].[SalesOrders] AS [Extent1]
    WHERE [Extent1].[Expiration] > (SysUtcDateTime())

Here it is clear that the SQL Server clock is used.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • Do you have a reference for it? – Hamid Pourjam Jan 08 '16 at 20:09
  • 1
    @dotctor he posted the query outputs and it jives with what I'm seeing and other answers that indicate the same. All you've done is give bad advice and try to close the question. – Eric Jan 08 '16 at 20:11
  • 1
    @doctor, This is the output I get from SQL Profiler. Check [Giorgi's answer](http://stackoverflow.com/a/34685436/2290059), it contains a reference. – Yacoub Massad Jan 08 '16 at 20:30
2

DateTime.UtcNow is mapped to CurrentUtcDateTime(). Here is a full list:

CLR Method to Canonical Function Mapping

Giorgi
  • 30,270
  • 13
  • 89
  • 125
-1

No. When you write a query which compares dates, EF creates a Datetime parameter, send it to the SQL Server, and the comparison is done by the server.

Adilson de Almeida Jr
  • 2,761
  • 21
  • 37
  • I want to believe that, but why would I get different results with the different code, if the dates are exactly the same? – Eric Jan 08 '16 at 20:06
  • Datetime.UtcNow specifically is rendered as a sql call to ´SysUtcDateTime()´. So in the second case you are using the SQL Server time. A tip to ease your next tests is to install MiniProfiler or any other tool which you can inspect the queries Entity Framework is performing. – Adilson de Almeida Jr Jan 08 '16 at 20:21