![](/img/trans.png)
[英]Using an intermediate entity in LINQ to Entities leads to System.NotSupportedException
[英]System.NotSupportedException using DateTime? in Linq to Entities
我有一个使用DateTime
的System.NotSupportedException
吗? 在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 to实体执行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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.