c# – 使用AddMonth方法的LINQ to Entities

这是我的代码:

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

我得到这个错误:

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

最佳答案
也许您可以将日期转换为测试对象:

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

转载注明原文:c# – 使用AddMonth方法的LINQ to Entities - 代码日志