系统.NotSupportedException使用日期时间?在链接到实体
本文关键字:链接 实体 时间 NotSupportedException 日期 系统 | 更新日期: 2023-09-27 18:19:14
我有一个System.NotSupportedException
使用DateTime
?in Linq to Entities
.
我最初的方法是这样的:
public TransportOrderLine GetLastTransportOrderLine(bool isDriver, int? driverId, int? haulierId , DateTime date, IDatabaseContext db)
{
var lines =
db.Query<TransportOrderLine>()
.Where(x => ((isDriver && x.TransportOrder.DriverId == driverId) ||
(!isDriver && x.TransportOrder.HaulierId == haulierId)) &&
x.StartDatePlanned.HasValue &&
EntityFunctions.TruncateTime(x.StartDatePlanned) <= date)
.ToList();
return lines.OrderByDescending(x => x.TransportOrder.Sequence)
.ThenByDescending(x => x.Sequence).LastOrDefault();
}
由于某种原因,当linq到实体执行DateTime
部分时,我有一个异常(NotSupportedException
)。
TransportOrderLine.StartDatePlanned
的类型是DateTime
?
我还像这样分割了我的代码:
var lines = db.Query<TransportOrderLine>()
.Where(x => ((isDriver && x.TransportOrder.DriverId == driverId) ||
(!isDriver && x.TransportOrder.HaulierId == haulierId)))
.ToList(); //ok
lines = lines.Where(x => x.StartDatePlanned.HasValue).ToList(); //ok
lines = lines.Where(x => EntityFunctions.TruncateTime(x.StartDatePlanned)<= date)
.ToList(); //error here
我也试过这个:
lines = lines.Where(x => (DateTime)EntityFunctions
.TruncateTime((DateTime)x.StartDatePlanned.Value)
.Value <= date).ToList(); //error here
谁都知道解决办法。
谢谢你的关注
这是我的解决方案:
var lines =
db.Query<TransportOrderLine>()
.Where(x => ((isDriver && x.TransportOrder.DriverId == driverId) ||
(!isDriver && x.TransportOrder.HaulierId == haulierId)) &&
x.StartDatePlanned.HasValue)
.ToList().Where(x => (DateTime)x.StartDatePlanned.Value <= date);