Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 941
Josh Lavely Avatar asked Feb 09 '17 18:02

Josh Lavely


1 Answers

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.

like image 81
Shane Ray Avatar answered Oct 24 '22 07:10

Shane Ray