DateTime是否在SQL Server评估的EF查询中运行,因为运行IL的机器会评估查询表达式之外的DateTime函数?
我有一个具有SalesOrders的应用程序
public class SalesOrder { public Int32 OrderID {get;set;} public DateTime Expiration {get;set;} }
我运行EF查询并在执行此操作时获得不同的结果:
DateTime utcnow = DateTime.UtcNow; var open = (from a in context.SalesOrders where a.Expiration > utcnow select a).ToList();
比我这样做:
var open = (from a in context.SalesOrders where a.Expiration > DateTime.UtcNow select a).ToList();
我认为这是因为实体框架查询中的DateTime.UtcNow由SQL Server评估,而DateTime.UtcNow在查询之外由运行IL的机器评估; 我基于这个答案.
我在Azure平台中作为服务,如果重要的话,使用Azure SQL DB在本地进行调试.
你的想法是正确的.
在SQL Server上,您的第一个查询运行以下SQL查询:
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'
这里很清楚客户端的时间是作为参数传递的.
您的第二个查询将其发送到SQL服务器:
SELECT [Extent1].[OrderID] AS [OrderID], [Extent1].[Expiration] AS [Expiration] FROM [dbo].[SalesOrders] AS [Extent1] WHERE [Extent1].[Expiration] > (SysUtcDateTime())
很明显,使用了SQL Server时钟.