LINQ to Entities不支持“TimeOfDay” - 在Linq 2 SQL中查找总秒数 [英] 'TimeOfDay' is not supported in LINQ to Entities - Find total seconds in Linq 2 SQL
问题描述
当我执行以下操作时:
[EdmFunction("Edm", "TruncateTime")]
public static DateTime? TruncateDateTime(DateTime? inputDate)
{
return inputDate.HasValue ? inputDate.Value.Date : (DateTime?)null;
}
[EdmFunction("Edm", "TotalSeconds")]
public static double? GetTotalSeconds(DateTime? inputDate)
{
return inputDate.HasValue ? inputDate.Value.TimeOfDay.TotalSeconds: (double?)null;
}
var employeeSelection = (from c in MyContext.Employees.Where(c => c.From.HasValue && c.To.TimeOfDay.TotalSeconds != 0)
...
select new Employee
{
To = c.To != DateTime.MinValue && c.To.TimeOfDay.TotalSeconds != 0 ? TruncateDateTime(c.To).Value :
c.To != DateTime.MinValue && c.To.TimeOfDay.TotalSeconds == 0 ? c.From.Value.AddMinutes(30) : DateTime.MinValue
}
我得到这个:
附加信息:LINQ to Entities中指定的类型成员TimeOfDay不是
,只有初始化器,实体成员和
实体导航属性。
Additional information: The specified type member 'TimeOfDay' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
当我尝试这个:
var employeeSelection = (from c in MyContext.Employees.Where(c => c.From.HasValue && GetTotalSeconds(c.To) != 0)
...
select new Employee
{
To = c.To != DateTime.MinValue && GetTotalSeconds(c.To) != 0 ? TruncateDateTime(c.To).Value :
c.To != DateTime.MinValue && GetTotalSeconds(c.To) == 0 ? c.From.Value.AddMinutes(30) : DateTime.MinValue
}
我得到:
附加信息:指定的方法
'System.Nullable1 [系统.Double]
1 [System.DateTime])'在类型
GetTotalSeconds(System.Nullable
'GetDateRangeMethod'不能被翻译成LINQ to Entities
存储表达式。
Additional information: The specified method 'System.Nullable
1[System.Double] GetTotalSeconds(System.Nullable
1[System.DateTime])' on the type 'GetDateRangeMethod' cannot be translated into a LINQ to Entities store expression.
如何找到Linq到SQL的总秒数?
How can I find the total seconds in Linq to SQL ?
感谢
推荐答案
尝试使用 SqlFunctions.DatePart 。
而不是
var employeeSelection = (from c in MyContext.Employees
.Where(c => c.From.HasValue && GetTotalSeconds(c.To) != 0)
更改为
var employeeSelection = (from c in MyContext.Employees
.Where(c => c.From.HasValue && SqlFunctions.DatePart("second", c.To) != 0)
这篇关于LINQ to Entities不支持“TimeOfDay” - 在Linq 2 SQL中查找总秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!