简体   繁体   English

System.NotSupportedException使用DateTime吗? 在Linq to实体

[英]System.NotSupportedException using DateTime? in Linq to Entities

I have a System.NotSupportedException using DateTime ? 我有一个使用DateTimeSystem.NotSupportedException吗? in Linq to Entities . Linq to Entities

My original method looks like this: 我的原始方法如下所示:

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();
}

For some reason, I'm having an exception ( NotSupportedException ) when linq to entities executes the DateTime part. 出于某种原因,当linq to实体执行DateTime部分时,我遇到了一个异常( NotSupportedException )。

TransportOrderLine.StartDatePlanned is of type DateTime ?. TransportOrderLine.StartDatePlanned的类型为DateTime ?。

I also split my code like this already: 我也已经像这样拆分了我的代码:

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

I also tried this: 我也试过这个:

lines = lines.Where(x => (DateTime)EntityFunctions
             .TruncateTime((DateTime)x.StartDatePlanned.Value)
             .Value <= date).ToList(); //error here

Anyone knows a solution. 任何人都知道解决方案。

Thanks for your attention :) 感谢您的关注 :)

This is my solution: 这是我的解决方案:

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.

相关问题 在LINQ to Entities中使用中间实体会导致System.NotSupportedException - Using an intermediate entity in LINQ to Entities leads to System.NotSupportedException LINQ to Entities:“选择新”表达式树将引发System.NotSupportedException - LINQ to Entities : “select new” expression tree throws System.NotSupportedException System.NotSupportedException:&#39;LINQ to Entities中不支持指定的类型成员&#39;StartDateTime&#39; - System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities ASP.NET MVC System.NotSupportedException LINQ到实体查询 - ASP.NET MVC System.NotSupportedException LINQ to Entities query 按日期时间过滤 LINQ 查询导致 System.NotSupportedException 错误 - Filtering a LINQ query by DateTime causing System.NotSupportedException Error linq“System.NotSupportedException”出错 - There is an error with linq “System.NotSupportedException” 使用 MongoDB.Driver.Linq 时出现 System.NotSupportedException - Getting a System.NotSupportedException when using MongoDB.Driver.Linq System.NotSupportedException: &#39;LINQ to Entities 不支持 LINQ 表达式节点类型&#39;Invoke&#39;。&#39; - System.NotSupportedException: 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.' DefaultIfEmpty()引起“ System.NotSupportedException:LINQ to Entities无法识别方法&#39;System.Collections.Generic.IEnumerable&#39;” - DefaultIfEmpty() Causing “System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable'” LINQ错误:GroupBy选择上出现System.NotSupportedException - LINQ error: System.NotSupportedException on GroupBy Selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM