LINQ to Entities with AddMonth method

Viewed 14474

This is my code:

 return Newsletterctx.Subscribers.Count(o =>
     o.Validated == false &&
     o.ValidationEmailSent == true &&
     o.SubscriptionDateTime.AddMonths(1) < DateTime.Now);

I get this error:

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

4 Answers

Now, EntityFramework, Version=6 above, you can jus use System.Data.Entity.DbFunctions

return Newsletterctx.Subscribers.Count(o =>
 o.Validated == false &&
 o.ValidationEmailSent == true &&
  DbFunctions.AddMonths(o.SubscriptionDateTime,1) < DateTime.Now);

However, in this case, use the temp variable testDate that answered by Fredrik Mörk uses less resources.

Related