score:7

Accepted answer

Try using the SqlFunctions.DateAdd method in the System.Data.Objects.SqlClient namespace. Documentation here. That will convert into the SQL method DateAdd, documented here. You might also be interested in using DateDiff instead, documented here.

In general, look at SqlFunctions for "common language runtime (CLR) methods that call functions in the database in LINQ to Entities queries." LINQ to Entities cannot convert any method call into SQL, but the functions in that class will work.

Your other option is to execute the LINQ to Entities query (using ToList or something similar) and then perform the logic in memory.

score:7

SqlFunctions only works with Microsoft Sql Server.

In pure EF you can write:

DbFunctions.AddMilliseconds(x.Date, DbFunctions.DiffMilliseconds(TimeSpan.Zero, x.Time))

This works on all database adapters


Related Articles