C# System.NotSupportedException是否使用DateTime?Linq中的实体
我有一个System.NotSupportedException使用DateTime?在Linq中创建实体 我最初的方法如下所示:C# System.NotSupportedException是否使用DateTime?Linq中的实体,c#,linq,entity-framework,datetime,notsupportedexception,C#,Linq,Entity Framework,Datetime,Notsupportedexception,我有一个System.NotSupportedException使用DateTime?在Linq中创建实体 我最初的方法如下所示: public TransportOrderLine GetLastTransportOrderLine(bool isDriver, int? driverId, int? haulierId , DateTime date, IDatabaseContext db) { var lines = db.Query<TransportOr
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();
}
我也试过:
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);
尝试EntityFunctions.TruncateTimex.StartDatePlanned.Value?Tim。唐,我已经试过了。请参阅最后一个代码块。请参阅:@IgorSemin EntityFunctions是专门为数据库知道的函数而存在的。@IgorSemin这将把LINQ2EF转换为LINQ2Object,sql server知道什么是EntityFunctions.TruncateTime
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);