LINQ to Entities with AddMonth method

后端 未结 4 797
渐次进展
渐次进展 2020-12-05 17:18

This is my code:

 return Newsletterctx.Subscribers.Count(o =>
     o.Validated == false &&
     o.ValidationEmailSent == true &&
     o.Su         


        
相关标签:
4条回答
  • 2020-12-05 17:52

    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.

    0 讨论(0)
  • 2020-12-05 18:04

    Perhaps you can shift the date to test against instead:

    DateTime testDate = DateTime.Now.AddMonths(-1);
    return Newsletterctx.Subscribers.Count
                (o => o.Validated == false 
                 && o.ValidationEmailSent == true 
                 && o.SubscriptionDateTime < testDate);
    
    0 讨论(0)
  • 2020-12-05 18:05

    You have to use the Datetime outside the request because you are in the LINQ TO ENTITIES that don't use System.Datetime Library.

    If you wish to use a fix date the you can define it outside the request as

    DateTime compareDate = DateTime.Now.AddMonths(x);

    0 讨论(0)
  • 2020-12-05 18:08

    You can use SqlFunctions classvar;

     today =  DateTime.Now; return Newsletterctx.Subscribers.Count(o =>
     o.Validated == false &&
     o.ValidationEmailSent == true &&
     SqlFunctions.DateAdd("month",1,o.SubscriptionDateTime) <today);
    
    0 讨论(0)
提交回复
热议问题