2

I have a table with two separate columns for date and time (SQL Server 2008).

I am trying to get the data between 1 minute before current time and 1 minute after current current time in my asp.net MVC (C#) application.

I have tried to use the where condition as:

ce.Start_Date.Add(ce.Start_Time) >= _currDateTime.AddMinutes(-1) && 
ce.Start_Date.Add(ce.Start_Time) <= _currDateTime.AddMinutes(1)

But from the above where condition, it throws an error:

The datepart millisecond is not supported by 
  date function dateadd for data type date.

How can I correct/rectify this error?

4 Answers 4

2

Don't add the minutes in the where clause, try something like this instead:

for ce in data
let start = _currDateTime.AddMinutes(-1)
let end = _currDateTime.AddMinutes(1)
where ce.Start_Date.Add(ce.Start_Time) >= start && 
ce.Start_Date.Add(ce.Start_Time) <= end
select ce

LINQ to SQL is attempting to translate your where clause into valid T-SQL (using T-SQL's dateadd function) and it is having trouble doing so.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried the above condition too and its not allowing to add ce.Start_Date.Add(ce.Start_Time) in where condition or even using let
1

The problem seems to be with server DATE type which does not have proper SQL translation.
Try to make LINQ convert Date to DateTime, something like this:

 Convert.ToDateTime(ce.Start_Date).Add(ce.Start_Time)

Comments

1

Converted to .AsEnumerable() and manipulate the date and time and return as .AsQueryable();

Comments

0

Simplest way to add "update time" field in table and compare current time with this field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.