3

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;
Community
  • 1
  • 1
Chris
  • 149
  • 1
  • 14
  • Rather then using `SqlFunctions.DatePart("ss", b.duration)` can't you use `SqlFunctions.DateAdd("ss", timespan.TotalSeconds, b.startTime` ; addin in the total seconds from TimeSpan? – Abhinav Galodha Apr 09 '16 at 22:34
  • 2
    Instead of the accepted answer, use the [rafael](http://stackoverflow.com/questions/16781587/using-datetime-addtimespan-with-linq/34346061#34346061)'s answer from the same thread. – Ivan Stoev Apr 10 '16 at 07:47

1 Answers1

5

As suggested in the comments I used:

DbFunctions.AddMilliseconds(b.startTime, DbFunctions.DiffMilliseconds(b.duration, TimeSpan.Zero)) >= endtime)
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Chris
  • 149
  • 1
  • 14