Chris
Chris

Reputation: 159

Code Example for Add DateTime and TimeSpan in EF query

I am looking for a code example of this question.

Using DateTime.Add(TimeSpan) with LINQ

I need to add an entire TimeSpan to a DateTime.

I already have tried SqlFunctions.DateAdd("ss", SqlFunctions.DatePart("s", b.duration) but this looks like it is only adding the the seconds part of the TimeSpan.

This is the code I have so far

var queryClash = from b in db.calEvents
                where (newEvent.startTime <= (SqlFunctions.DateAdd("ss", SqlFunctions.DatePart("ss", b.duration), b.startTime)))
                && (newEventEndTime >= b.startTime)
                select b;

Upvotes: 3

Views: 2488

Answers (1)

Chris
Chris

Reputation: 159

As suggested in the comments I used:

DbFunctions.AddMilliseconds(b.startTime, DbFunctions.DiffMilliseconds(b.duration, TimeSpan.Zero)) >= endtime)

Upvotes: 5

Related Questions