1

我有一个System.NotSupportedException使用DateTime?在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();
}

NotSupportedException出于某种原因,当 linq to entity 执行该DateTime部分时,我遇到了异常 ( )。

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

任何人都知道解决方案。

感谢您的关注 :)

4

1 回答 1

0

这是我的解决方案:

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);
于 2014-08-22T11:34:20.067 回答