有时我们要对日期做加减运算,常用于where语句中:

//错误代码
Context.Article
    .Where(p => p.StartDate < DateTime.Now)
    .Where(p => p.StartDate.AddDays(p.Period) > DateTime.Now)

以上代码是程序员很自然的想法,可惜的是EF不支持AddDays方法。需要使用EntityFunctions:

Context.Article.Where(p => p.StartDate < DateTime.Now)
               .Where(p => EntityFunctions.AddDays(p.StartDate, p.Period)
                   > DateTime.Now);

 

 

相关文章:

  • 2022-01-02
  • 2021-12-06
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2021-12-10
  • 2021-10-02
相关资源
相似解决方案