【问题标题】:How to fix "LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'"?如何修复“LINQ to Entities 无法识别方法‘System.DateTime GetValueOrDefault()’”?
【发布时间】:2020-03-27 04:36:53
【问题描述】:

我有这个代码。基本上我需要更改日期并告诉用户今天,商店将从上午 10 点营业到晚上 10 点。所以这里的时间是固定的。

来自:-

tod​​ay_date = "2020-03-23T00:00:00"

start_hour_time = "1900-01-01T10:00:00"

end_hour_time = "1900-01-01T22:00:00"

改为:-

start_newdatetime = "2020-03-23T10:00:00"

end_newdatetime = "2020-03-23T22:00:00"

DateTime todaysdate = DateTime.Now.Date;       

var find_date = bdb.tbl_store
        .Join(bdb.tbl_hour, a => a.start_hour_id, b => b.hour_id, (a, b) => new { a, b })
        .Join(bdb.tbl_hour, a => a.a.end_hour_id, b => b.hour_id, (a, b) => new { a, b })
        .Where(x => x.a.a.store_id == store_id )
        .Select(x => new {

        x.a.a.store_id,
        start_hour_time = x.a.b.hour_time,
        end_hour_time = x.b.hour_time,
        start_newdatetime = todaysdate.Date+ x.a.b.hour_time.GetValueOrDefault().TimeOfDay,
        end_newdatetime = todaysdate.Date + x.b.hour_time.GetValueOrDefault().TimeOfDay

    }) .ToList();

现在的问题,是错误。因为我希望它在同一个变量上。

"Message": "发生错误。", "ExceptionMessage": "LINQ to 实体无法识别方法“System.DateTime
GetValueOrDefault()' 方法,该方法不能翻译成 一个商店表达式。", "ExceptionType": "System.NotSupportedException",

但是如果我创建另一个 var 来返回新值就可以了。

【问题讨论】:

  • 根据this answer判断,您应该可以使用(x.a.b.hour_time ?? TimeSpan.Zero)
  • 我已经从那个答案中尝试过了,它不起作用。我不能简单地添加??在 x.a.b.hour_time 之后,因为它会返回错误。
  • 返回什么错误?
  • 在这种情况下,我将投票重新打开,但您也应该编辑您的问题以提及这个新错误。 :)
  • 旁注:只运行该操作客户端(不在 SQL 中)会容易得多...

标签: c# linq datetime linq-to-entities


【解决方案1】:

您的表达式似乎不起作用,因为成员 GetValueOrDefaultTimeOfDay 不适用于 EF DB 查询翻译,因此您必须以某种方式解决该问题。目前我无法验证,但我认为以下应该是可能的

var find_date = bdb.tbl_store
        .Join(bdb.tbl_hour, a => a.start_hour_id, b => b.hour_id, (a, b) => new { a, b })
        .Join(bdb.tbl_hour, a => a.a.end_hour_id, b => b.hour_id, (a, b) => new { a, b })
        .Where(x => x.a.a.store_id == store_id )
        .ToList()
        .Select(x => new {
            x.a.a.store_id,
            start_hour_time = x.a.b.hour_time,
            end_hour_time = x.b.hour_time,
            start_newdatetime = todaysdate.Date + x.a.b.hour_time.GetValueOrDefault().TimeOfDay,
            end_newdatetime = todaysdate.Date + x.b.hour_time.GetValueOrDefault().TimeOfDay
    }).ToList();

这样,您的查询在调用 ToList 时运行,其他所有内容(传递给 Select 的 Lambda 表达式)在客户端上作为纯 C# 执行,因此 Entity 确实没有理由抱怨。诚然,我不确定这些对象在调用ToList 时是否已完全解决。如果不是这样,你可以这样做

var find_date = bdb.tbl_store
        .Join(bdb.tbl_hour, a => a.start_hour_id, b => b.hour_id, (a, b) => new { a, b })
        .Join(bdb.tbl_hour, a => a.a.end_hour_id, b => b.hour_id, (a, b) => new { a, b })
        .Where(x => x.a.a.store_id == store_id)
        .Select(x => new {
            x.a.a.store_id,
            start_hour_time = x.a.b.hour_time,
            end_hour_time = x.b.hour_time 
        })
        .ToList()
        .Select(x => new {
            store_id,
            start_hour_time,
            end_hour_time,
            start_newdatetime = todaysdate.Date + start_hour_time.GetValueOrDefault().TimeOfDay,
            end_newdatetime = todaysdate.Date + end_hour_time.GetValueOrDefault().TimeOfDay
        }).ToList();

【讨论】:

  • 是的!第一个效果很好。第二个也可以,但它会是多余的并且会产生冗长的代码。谢谢你!! ^_^
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
相关资源
最近更新 更多