I have a SQL Server 2016 table of events where the event date and start time are stored in separate columns. I'm trying to write a query to identify which of a provided list of DateTime
s already exist in the table so I need to add or combine the EventDate
and StartTime
columns before doing the comparison like this:
public List<DateTime> DoEventsExist(List<DateTime> dateBatch)
{
try
{
DBContext.Events.Where(ce => dateBatch.Contains(ce.EventDate.Add(ce.StartTime));
}
catch (Exception ex)
{
}
}
But this, of course, would give an exception with the message:
LINQ to Entities does not recognize the method 'System.DateTime Add(System.TimeSpan)' method, and this method cannot be translated into a store expression.
In this case the columns are of Date
and Time
types respectively (so the .NET types are DateTime
and TimeSpan
), but I'd also like to know how to accomplish this if they were both DateTime2
types where one contained a date at midnight and the other contained an irrelevant date with the correct time.
SqlFunctions.DateAdd
would probably work but it would make my code tightly coupled with MS SQL Server, which I don't want.
Note There is a similar question, however it does not ask about combining two DateTimes. Additionally it is not specific to EF 6, its answer predates EF 6, and the answer does not work so the whole question is useless to SO.