4

In dev environment the following SQL statement is working but not on any other environment:

System.NotSupportedException: LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.

Related code:

bool hasSomethinglast14days = (from a in db.someTable
                               where a.SomeIntColumn == someIntVariable
                                  && a.CreateDate >= DateTime.UtcNow.AddDays(-14)
                               select a.someColumn).Any();
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
kEpEx
  • 830
  • 11
  • 20
  • 1
    I'd be really curious to know how this works in the dev environment. Is there something drastically different about the database there? Or some kind of different entity provider of some kind? I wouldn't expect this to work in any expression tree that has to be realized against a database. – David Dec 10 '14 at 02:31

1 Answers1

6

The provider you're using is unable to translate the AddDays() method into a valid SQL statement.

Since the date you're subtracting from doesn't depend on any values from the database anyway, just do your subtraction first (outside of the query), then use the resulting value:

var pastDate = DateTime.UtcNow.AddDays(-14);

bool hasSomethinglast14days = (from a in db.someTable
                               where a.SomeIntColumn == someIntVariable
                                  && a.CreateDate >= pastDate
                               select a.someColumn).Any();

As for it working in your Dev environment, I don't have an answer for that. I'm surprised it does.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Even do I knew the asnwer, it seems like it never worked on Dev environment in the first place. Thanks! – kEpEx Dec 10 '14 at 02:57