Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canonical Function in Linq to Entities not working

Tags:

c#

sql

linq

Trying to run this query in my code:

var rslt = ent.AppointmentDiaries.Where(s => s.DateTimeScheduled >=
                fromDate && EntityFunctions.AddMinutes(
                s.DateTimeScheduled, s.AppointmentLength) <= toDate);

It keeps breaking up with:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.Nullable`1[System.DateTime] AddMinutes(System.Nullable`1[System.DateTime], System.Nullable`1[System.Int32])' method, and this method cannot be translated into a store expression.

using entityframework 6.1.0 ...

I know about canonical gunctions, so I would expect EntityFunctions.AddMinutes to work with codefirst queries...

Any idea what I am doing wrong?

like image 349
Evangelos Aktoudianakis Avatar asked Mar 20 '23 18:03

Evangelos Aktoudianakis


2 Answers

In Entity Framework 6 EntityFunctions has been replaced by DbFunctions. The old class is now marked as obsolete and will disappear in the future.

I must admit that it's not directly obvious when you google for release notes. There is however a blog describing how to upgrade to EF6 where this change is mentioned.

like image 85
Gert Arnold Avatar answered Mar 23 '23 07:03

Gert Arnold


Yes it's just like @Gert said. Here is the code for it:

var rslt = ent.tbl_Appoiment_Diary.Where(s => s.DateTimeScheduled >= fromDate && DbFunctions.AddMinutes(s.DateTimeScheduled, s.AppointmentLenght) <= toDate);

Hope this helps. Thx again for the info @Gert! Cheers.

like image 42
Jon A Avatar answered Mar 23 '23 06:03

Jon A