NHibernate.Linq查询问题,向DateTime添加秒

时间:2015-08-27 13:31:06

标签: c# linq entity-framework nhibernate fluent-nhibernate

使用NHibernate.Linq和Entity Framework 6.0时,我收到以下异常。

NHibernate.QueryException:无法在没有投影的条件下使用子查询。

问题特别来自:

taco.LastRun <= DbFunctions.AddSeconds(taco.LastRun, taco.PeriodSeconds)

当我删除该部分代码时,结果会变得完美。

var results = (
    from taco in Session.Linq<Taco>()
    where
        taco.IsActive && !taco.IsProcessing &&
        ((taco.StartCrunching == null) || (taco.StartCrunching <= currentDateTime)) &&
        ((taco.Cycles == null) || (taco.CycleCount < taco.Cycles)) &&
        (taco.LastRun == null || (taco.LastRun <= DbFunctions.AddSeconds(taco.LastRun, taco.PeriodSeconds))) &&
        ((taco.EndCrunching == null))
    select taco).ToList();

1 个答案:

答案 0 :(得分:0)

我认为这是因为您正在更新select查询中的信息。 此更新本身就是某种查询(可能是UPDATE SQL语句)。

首先尝试更新信息,然后执行选择查询;

看看你正在尝试做什么虽然没有多大意义,如

taco.LastRun&lt; = taco.LastRun + taco.PeriodSeconds

将永远为真。(提供taco.PeriodSeconds> 0)。 但我想我知道你要做的是什么:

var results = (
from taco in Session.Linq<Taco>()
where
    taco.IsActive && !taco.IsProcessing &&
    ((taco.StartCrunching == null) || (taco.StartCrunching <= currentDateTime)) &&
    ((taco.Cycles == null) || (taco.CycleCount < taco.Cycles)) &&
    (taco.LastRun == null || (taco.LastRun <= currentDateTime.AddSeconds(taco.PeriodSeconds)))
    && ((taco.EndCrunching == null))
select taco).ToList();

您是否正在尝试检查LastRun是否小于当前时间+ PeriodSeconds?以上就是这样做的。

还应该注意,您不需要同时使用Entity Framework和NHibernate。它们都是ORM,因此它们都做了非常相似的事情。