【问题标题】:TimeSpan: The specified type member 'TotalHours' is not supported in LINQ to EntitiesTimeSpan:LINQ to Entities 不支持指定的类型成员“TotalHours”
【发布时间】:2014-03-18 01:53:45
【问题描述】:

我有这个代码

var late_list = context.Results
                    .Where(r => ((TimeSpan)r.time_late).TotalHours > 0)
                    .ToList();

它不喜欢TotalHours 的东西。错误是:“LINQ to Entities 不支持指定的类型成员 'TotalHours'”

那么,如何使用时间跨度进行查询? (SQL 中的“时间”类型)。

【问题讨论】:

    标签: c# .net linq entity-framework linq-to-entities


    【解决方案1】:

    我只是用一个新的时间跨度来比较。

    TimeSpan zero_time = new TimeSpan(0, 0, 0);
    var late_list = context.Results.Where(r => ((TimeSpan)r.time_late) > zero_time).ToList();
    

    【讨论】:

      【解决方案2】:

      请查看System.Data.Entity.DbFunctions

      我有一个 where 子句,我不得不忽略 DateTime where 子句中的时间。

      .Where(t => System.Data.Entity.DbFunctions.TruncateTime(t.LogTimeStamp) >= fromDate)
      .Where(t => System.Data.Entity.DbFunctions.TruncateTime(t.LogTimeStamp) <= toDate)
      

      对于您的情况,请查看DbFunctions.DiffHours Methods 并考虑使用 EntityFramework 6。当您使用 TotalHours 时,它可能不会直接帮助您。

      【讨论】:

        猜你喜欢
        • 2015-01-27
        • 2012-07-17
        • 2013-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        • 2018-02-19
        • 2015-07-25
        相关资源
        最近更新 更多