简体   繁体   中英

Compare DateTime in EF Core Linq Query

I am using .NETCore and EF Core in my project. I am unable to query items with expressions using DateTimes.

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

The CPULoad works perfect but the datetime comparions outputs the below.

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

Can someone explain what i'm doing wrong? Thanks in advance!

try this...

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

essentially the problem here is entity framework does not know how to convert DateTime.Now.AddMinutes(-5) to SQL, so you need to get the value and then pass that to entity framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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