我收到了NotSupportedException
:
方法'System.TimeSpan FromMinutes(Double)'没有支持的SQL翻译。
运行此查询时:
var searchSchedules =
from searchSchedule in db.SearchSchedules
where searchSchedule.Active.Value &&
searchSchedule.LastChecked.Value <
DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(
searchSchedule.CheckFrequencyMinutes)) &&
searchSchedule.PublishingConfigurationId == pubConfig.Id
select searchSchedule;
显然,问题出在查询的TimeSpan
部分。我怎样才能做到这一点?
答案 0 :(得分:4)
如果您只是想检查自上次检查后是否已经过了指定的分钟数,您可以像这样重写:
var searchSchedules =
from searchSchedule in db.SearchSchedules
where searchSchedule.Active.Value &&
((DateTime.UtcNow - searchSchedule.LastChecked.Value).Minutes >= searchSchedule.CheckFrequencyMinutes) &&
searchSchedule.PublishingConfigurationId == pubConfig.Id
select searchSchedule;
答案 1 :(得分:1)
执行此操作的一种方法是将此查询分为两部分。一件针对数据库执行,第二件针对客户端执行,如下所示:
// This executes against the database
var query1 = from searchSchedule in db.SearchSchedules
where searchSchedule.Active.Value
&& searchSchedule.PublishingConfigurationId == pubConfig.Id
select searchSchedule;
// This executes on the client
var query2 = from query1 as IEnumerable()
where query1.LastChecked.Value < DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(query1.CheckFrequencyMinutes))
当我想在SQL Server不知道如何处理的查询中包含一个函数时,我经常会这样做。在第二个查询中,将query1转换为IEnumerable会强制它在客户端上针对.NET框架而不是数据库执行。
您需要确定客户端上可能需要处理的行数,以确定它的效率。