Apologies it's probably a poor title; I have a question more out of intrigue than anything.
I have tested the same LINQ to Entities statement wrote in 2 different ways, one using Datetime.Now and one using a date variable:
var timeNow = DateTime.Now;
var pendingMailshots = db.MailshotHistoryDatas.Where(m =>
m.SendDate < timeNow).ToList();
var pendingMailshots = db.MailshotHistoryDatas.Where(m =>
m.SendDate < DateTime.Now).ToList();
I noticed there were discrepencies in the data they were finding and after some digging and some profiling I found this:
exec sp_executesql N'SELECT
[Extent1].[MailshotGuid] AS [MailshotGuid],
[Extent1].[MailshotLineId] AS [MailshotLineId],
[Extent1].[SendDate] AS [SendDate],
[Extent1].[MessageType] AS [MessageType],
[Extent1].[SendStatus] AS [SendStatus],
[Extent1].[Recipients] AS [Recipients],
[Extent1].[SendAttempts] AS [SendAttempts],
[Extent1].[DateSent] AS [DateSent]
FROM [dbo].[MailshotLineDatas] AS [Extent1]
INNER JOIN [dbo].[MailshotDatas] AS [Extent2] ON [Extent1].[MailshotGuid] = [Extent2].[MailshotGuid]
WHERE ([Extent1].[SendDate] < @p__linq__0),N'@p__linq__0 datetime2(7),@p__linq__0='2018-01-04 15:11:26.5618636'
SELECT
[Extent1].[MailshotGuid] AS [MailshotGuid],
[Extent1].[MailshotLineId] AS [MailshotLineId],
[Extent1].[SendDate] AS [SendDate],
[Extent1].[MessageType] AS [MessageType],
[Extent1].[SendStatus] AS [SendStatus],
[Extent1].[Recipients] AS [Recipients],
[Extent1].[SendAttempts] AS [SendAttempts],
[Extent1].[DateSent] AS [DateSent]
FROM [dbo].[MailshotLineDatas] AS [Extent1]
INNER JOIN [dbo].[MailshotDatas] AS [Extent2] ON [Extent1].[MailshotGuid] = [Extent2].[MailshotGuid]
WHERE([Extent2].[StartDate] < (SysDateTime())))
Using the datetime variable it runs the query as a stored procedure and using Datetime.Now it converts the code into TSQL.
Can anyone explain why this is? Also, what would you consider better practice?
Thanks in advance, Adam