0

I am getting error while using EntityFunctions.DiffMinutes() with MySQL. Below is my code

return db.DiscoveredDevices.Where(m => EntityFunctions.DiffMinutes((DateTime)m.LastPollTime, DateTime.Now) <= pollTime && m.Status == true).ToList();

this function takes two dateTime objects and returns difference in minutes. this works fine in MSSQL but shows error "DiffMinutes does not exist" when using with MySQl.

If I use my custom or any built in DateTime method than it throws exception "LINQ to Entities does not recognize this method "

I will be grateful if somebody helps me in this

regards Umair Zaman

Umair Zaman
  • 341
  • 1
  • 2
  • 13

2 Answers2

0

The DiffMinutes function doesn't exist in MySQL, just create it and will work:

CREATE FUNCTION `DiffMinutes`(timeValue1 datetime, timevalue2 datetime) RETURNS int(11)
    DETERMINISTIC
BEGIN
RETURN TIMESTAMPDIFF(MINUTE, timeValue1, timevalue2);
END
Péter Aradi
  • 370
  • 1
  • 4
  • 12
0

I think maybe MySQL doesn't implement that Function...

you can use the other way like this:

    DateTime begin = DateTime.Now - pollTime;
    DateTime end = DateTime.Now + pollTime;

    var result = (from s in db.DiscoveredDevices where s.LastPollTime > begin && s.LastPollTime < end && && s.Status == true select s).ToList();
    return result;
Maidot
  • 386
  • 4
  • 11