Comparing DateTime in Entity Framework with an Sql

2019-09-09 05:54发布

Why this code throws a System.NotSupportedException telling that The specified method 'int? DateDiff(string, DateTime?, DateTime?)' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression, though DateDiff has EdmFunction attribute?

context.Users.Where(f => System.Data.Objects.SqlClient.SqlFunctions.DateDiff("second", f.LastLogOn, somedatetime) < 0)

1条回答
Evening l夕情丶
2楼-- · 2019-09-09 06:51

Here is a complete enumeration of canonical Date and Time functions supported by Entity SQL query provider link.

Try something like this

context.Users.Where(f=>EntityFunctions.DiffSeconds(f.LastLogOn, somedatetime)<0);
查看更多
登录 后发表回答