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

后端 未结 3 1877
-上瘾入骨i
-上瘾入骨i 2020-12-21 17:10

When I do the following :

[EdmFunction(\"Edm\", \"TruncateTime\")]
public static DateTime? TruncateDateTime(DateTime? inputDate)
{
    return inputDate.HasV         


        
相关标签:
3条回答
  • 2020-12-21 17:46

    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)))

    0 讨论(0)
  • 2020-12-21 17:55

    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.

    0 讨论(0)
  • 2020-12-21 17:58

    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.

    0 讨论(0)
提交回复
热议问题