【问题标题】:Subtract DateTimes in IQueryable query在 IQueryable 查询中减去 DateTimes
【发布时间】:2013-09-17 16:01:37
【问题描述】:

您好,我正在尝试在查询中减去两个 DateTime,并将差异与 timeInterval 进行比较,但我得到了例外:

 <Message>An error has occurred.</Message>
 <ExceptionMessage>System.TimeSpan Subtract(System.DateTime)</ExceptionMessage>
 <ExceptionType>System.NotSupportedException</ExceptionType>

C#代码:

 producerRelations = producerRelationRepository.Query().Fetch(c => c.Order).
                Where(c => c.Order.CreatedBy == login).
                Where(c=>currentDate.Subtract(c.RouteUnit.DtStart.Value).TotalMinutes<timeInterval);    

如何在我的代码中减去日期?

【问题讨论】:

    标签: c# .net linq iqueryable


    【解决方案1】:

    使用AsEnumerable

     producerRelations = producerRelationRepository.Query().Fetch(c => c.Order).
                    Where(c => c.Order.CreatedBy == login).AsEnumerable().
                    Where(c=>currentDate.Subtract(c.RouteUnit.DtStart.Value).TotalMinutes<timeInterval);
    

    或者你需要使用EntityFunctions

    【讨论】:

    • 发生错误。 System.Nullable1[System.Int32] DiffMinutes(System.Nullable1[System.DateTime], System.Nullable`1[System.DateTime]) System.NotSupportedException
    • @Abbath 你最好在 where 子句中包含 currentDate.HasValue &amp;&amp; c.RouteUnit.DtStart.HasValue ,然后再添加 DiffMinutes
    • 你不要只是将日期时间转换为毫秒,然后减去它们,然后将毫秒转换回日期时间
    • 什么转换成毫秒?我使用 minits 和 timeIntervalis 双值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多