0

Given the timezone offset value as a nullable int how would I go about converting that inside the linq query to the current time of the location?

 var data = (from c in dbContext.Contacts
                      select new
                        {
                            c.Id,
                            TheirTime = DateTime.UtcNow.AddHours(c.TimezoneOffset)
                     })

This returns a "LINQ to Entities does not recognize the method 'System.DateTime AddHours(Double)' method, and this method cannot be translated into a store expression." error.

John S
  • 7,909
  • 21
  • 77
  • 145
  • You might want to check the answer to my question here (http://stackoverflow.com/questions/18901025/custom-extension-method-to-simplify-linq-to-sql-statement). – Ani Jun 03 '14 at 17:15
  • Also http://stackoverflow.com/questions/10717177/how-to-query-and-calculate-dates-in-the-where-clause-of-a-linq-statement – Jon Jun 03 '14 at 17:25

1 Answers1

1

You can use EntityFunctions for this specific query:

var data = (from c in dbContext.Contacts
                      select new
                        {
                            c.Id,
                            TheirTime =EntityFunctions.AddHours(DateTime.UtcNow,c.TimezoneOffset)
                     })
Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17