如何修复 "LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'"?
How to fix "LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'"?
我有这个代码。基本上我需要更改日期并告诉用户今天商店将从上午 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": "An error has occurred.", "ExceptionMessage": "LINQ to
Entities does not recognize the method 'System.DateTime
GetValueOrDefault()' method, and this method cannot be translated into
a store expression.", "ExceptionType":
"System.NotSupportedException",
但如果我为 return 新值创建另一个变量就没问题了。
您的表达式似乎不起作用,因为成员 GetValueOrDefault
和 TimeOfDay
不可用于 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# 执行,因此确实存在实体没有理由抱怨。诚然,我不确定调用 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();
我有这个代码。基本上我需要更改日期并告诉用户今天商店将从上午 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": "An error has occurred.", "ExceptionMessage": "LINQ to Entities does not recognize the method 'System.DateTime
GetValueOrDefault()' method, and this method cannot be translated into a store expression.", "ExceptionType": "System.NotSupportedException",
但如果我为 return 新值创建另一个变量就没问题了。
您的表达式似乎不起作用,因为成员 GetValueOrDefault
和 TimeOfDay
不可用于 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# 执行,因此确实存在实体没有理由抱怨。诚然,我不确定调用 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();