Categories
c# entity-framework sql

C#: Why is one of my .Where() conditions missing in my SQL query?

I have a method with the following line of code being run. It’s supposed to get all the messages that have been created within the past 24 hours and aren’t created in a future time (because that shouldn’t be possible).

messages.AddRange(_dbcontext.Messages
.Where(message => message.CreationTime >= DateTime.UtcNow.AddHours(-24) && message.CreationTime <= DateTime.UtcNow)
.ToList());

When the application runs and passes the above line, this is the SQL Query that ran:

SELECT [message].[Id], [message].[CreationTime], [message].[Value]
FROM [Messages] AS [message]
WHERE([message].[CreationTime] <= GETUTCDATE())

This basically retrieves all messages, instead of those created within the last 24 hours.

I want to know why this part of the Where() is being ignored or not being transformed to an SQL query, and what I can do to make it work.

(message => message.CreationTime >= DateTime.UtcNow.AddHours(-24)