繁体   English   中英

在EF Core Linq查询中比较DateTime

[英]Compare DateTime in EF Core Linq Query

我在项目中使用.NETCore和EF Core。 我无法使用DateTimes查询带有表达式的项目。

return _context.table
       .Where( x=> x.CPULoad > 90 && X.Date >= DateTime.Now.AddMinutes(-5));

CPULoad可以完美工作,但datetime比较输出以下内容。

The LINQ expression '(([x].Date >= DateTime.Now.AddMinutes(-5))' could not be translated and will be evaluated locally.

有人可以解释我在做什么错吗? 提前致谢!

尝试这个...

var compareDate = DateTime.Now.AddMinutes(-5);
return _context.table
    .Where( x=> x.CPULoad > 90 && X.Date >= compareDate);

本质上,这里的问题是实体框架不知道如何将DateTime.Now.AddMinutes(-5)转换为SQL,因此您需要获取该值,然后将其传递给实体框架。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM