简体   繁体   English

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

[英]NHibernate.Linq Query Issue with adding seconds to DateTime

I am receiving the following exception while using NHibernate.Linq and Entity Framework 6.0. 使用NHibernate.Linq和Entity Framework 6.0时,我收到以下异常。

NHibernate.QueryException: Cannot use subqueries on a criteria without a projection. NHibernate.QueryException:如果没有投影,则无法在条件上使用子查询。

The issue is specifically coming from: 该问题具体来自:

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

When I remove that section of code, the results come back perfect. 当我删除那部分代码时,结果又完美了。

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

I think it's because you're updating information inside a select query. 我认为这是因为您正在更新选择查询中的信息。 This update will itself be a query of some kind(probably an UPDATE SQL statement). 此更新本身将是某种查询(可能是UPDATE SQL语句)。

Try updating the information first then doing your select query; 首先尝试更新信息,然后进行选择查询;

looking at what you're trying to do though doesn't make much sense, as 看着你想做什么虽然没有多大意义,因为

taco.LastRun <= taco.LastRun + taco.PeriodSeconds taco.LastRun <= taco.LastRun + taco.PeriodSeconds

will always be true.(Provided taco.PeriodSeconds > 0 ). 始终为true。(提供taco.PeriodSeconds> 0)。 But I think I know what you're trying to do: 但我想我知道您要做什么:

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

you're trying to check if the LastRun was less than the current time + PeriodSeconds ? 您正在尝试检查LastRun是否小于当前时间+ PeriodSeconds? The above will do that. 以上将做到这一点。

Also it should be noted that you don't need to use both Entity Framework and NHibernate. 还应注意,您不需要同时使用Entity Framework和NHibernate。 They're both ORM's and so they both do very similar things. 它们都是ORM,所以它们都做非常相似的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM