【发布时间】:2020-03-27 04:36:53
【问题描述】:
我有这个代码。基本上我需要更改日期并告诉用户今天,商店将从上午 10 点营业到晚上 10 点。所以这里的时间是固定的。
来自:-
today_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