'TimeOfDay' is not supported in LINQ to Entities - Find total seconds in Linq 2 SQL

不问归期 提交于 2019-11-29 15:46:12

Try using SqlFunctions.DatePart.

e.g. instead of

var employeeSelection = (from c in MyContext.Employees
          .Where(c => c.From.HasValue && GetTotalSeconds(c.To) != 0)

try something like

var employeeSelection = (from c in MyContext.Employees
          .Where(c => c.From.HasValue && SqlFunctions.DatePart("second", c.To) != 0)

For NetMage

var employeeSelection = (from c in MyContext.Employees
          .Where(c => c.From.HasValue && (SqlFunctions.DatePart("second", c.To) + (60 * SqlFunctions.DatePart("minute", c.To)) + (3600 * SqlFunctions.DatePart("hour", c.To))) != 0)

Where

TotalSeconds = (SqlFunctions.DatePart("second", c.To) + (60 * SqlFunctions.DatePart("minute", c.To)) + (3600 * SqlFunctions.DatePart("hour", c.To)))

Have a look at Date and Time Canonical Functions for LINQ-to-Entities and if you are unsure on how to call them, have a look at How to: Call Canonical Functions

The methods that are available should be able to solve the problem your currently having where the operations your currently using are not supported by the database.

If you have a Linq-To-Entities Query you can only perform operations which can be executed by your used database management system.

You will need to get your desired information from your DB and create a list from that query, than that will be called Linq-To-Objects and you will be able to perform you desired operation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!