请问EF中对可为null的日期字段进行判断时如何转换?

romanchaos 2014-06-24 06:03:17
linq to sql直接convert.todatetime就行了,ef用date.value不行,用System.Data.Objects.EntityFunctions.DiffDays和System.Data.Objects.SqlClient.SqlFunctions.DateDiff都直接报:

LINQ to Entities 不识别方法“System.Nullable`1[System.Int32] DiffDays(System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])”,因此该方法无法转换为存储表达式。

LINQ to Entities 不识别方法“System.Nullable`1[System.Int32] DateDiff(System.String, System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])”,因此该方法无法转换为存储表达式。

请问该用什么方法?以下是linq语句:

var str = from p in db.UnsalableSalesRptReasons
where !p.UnsalableRpt.IsFiling
&& p.CeateTime != null
//&& System.Data.Objects.EntityFunctions.DiffDays(p.CeateTime, DateTime.Now) > 30
&& System.Data.Objects.SqlClient.SqlFunctions.DateDiff("D", p.CeateTime, DateTime.Now) > 1
select p.UnsalableRpt.ItemId;

其中p.CeateTime是datetime?类型
...全文
641 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
romanchaos 2014-06-26
  • 打赏
  • 举报
回复
引用 7 楼 save4me 的回复:
参考: LINQ + EntityFunctions in asp.net mvc
引用
However, Entity Framework 6 has removed the dependency on System.Data.Entity and has redefined all necessary classes for that purpose inside of the EntityFramework assembly. Meaning that, in the case of your console app, I am guessing that either by design (System.Data.Entity is not referenced) or by accident (System.Data.Entity is referenced but the EntityFunctions class is taken from EntityFramework.dll), the correct version of EntityFunctions (from EntityFramework.dll) is taken. If you are using any version of Entity Framework 6, make sure that you are using the EntityFunctions class that can be found in EntityFramework.dll, not the one in System.Data.Entity. Source code of EntityFunctions.cs in Entity Framework 6 Actually, if you use Entity Framework 6, I would recommend removing all and any references to System.Data.Entity - in order to avoid any future confusion and mistake.
因为Entity Framework 6把System.Data.Entity的依赖关系已经移除了,放到了EntityFramework.dll,他建议在Entity Framework 6把所有的System.Data.Entity的引用删除,避免歧义和错误。你也试一下。 [quote=引用 5 楼 romanchaos 的回复:] [quote=引用 4 楼 dongxinxi 的回复:] 在前面加个条件 && p.CreatedDate.HasValue (对应SQL语句的 is not null) && SqlFunctions.DateDiff("D", p.CreatedDate, DateTime.Now)
不行,他还是报无法转换为存储表达式的异常,其实问题应该是出在ef生成sql语句的过程中,它不会管前面是否有HasValue的,而是将HasValue和DateDiff都转成sql语句,但我网上搜来搜去别人都是直接用EntityFunctions.DiffDays或者SqlFunctions.DateDiff就可以的,但在我这就报错。另外我用的是EF6.0会不会有版本问题?或者说还要引入一些什么dll?[/quote][/quote] thx~明天试试~
romanchaos 2014-06-26
  • 打赏
  • 举报
回复
引用 6 楼 sp1234 的回复:
使用sql语句进行查询吧。 你不会学了EF,就把直接的ado.net给忘记了吧。
没办法……团队还停留在sql语句散布在三层的级别,这是我做的一个同步器顺用于踩坑,打算重构时集体转EF……
save4me 2014-06-26
  • 打赏
  • 举报
回复
谢谢分享问题的解决方法
引用 10 楼 romanchaos 的回复:
[quote=引用 7 楼 save4me 的回复:] 参考: LINQ + EntityFunctions in asp.net mvc
引用
However, Entity Framework 6 has removed the dependency on System.Data.Entity and has redefined all necessary classes for that purpose inside of the EntityFramework assembly. Meaning that, in the case of your console app, I am guessing that either by design (System.Data.Entity is not referenced) or by accident (System.Data.Entity is referenced but the EntityFunctions class is taken from EntityFramework.dll), the correct version of EntityFunctions (from EntityFramework.dll) is taken. If you are using any version of Entity Framework 6, make sure that you are using the EntityFunctions class that can be found in EntityFramework.dll, not the one in System.Data.Entity. Source code of EntityFunctions.cs in Entity Framework 6 Actually, if you use Entity Framework 6, I would recommend removing all and any references to System.Data.Entity - in order to avoid any future confusion and mistake.
因为Entity Framework 6把System.Data.Entity的依赖关系已经移除了,放到了EntityFramework.dll,他建议在Entity Framework 6把所有的System.Data.Entity的引用删除,避免歧义和错误。你也试一下。 [quote=引用 5 楼 romanchaos 的回复:] [quote=引用 4 楼 dongxinxi 的回复:] 在前面加个条件 && p.CreatedDate.HasValue (对应SQL语句的 is not null) && SqlFunctions.DateDiff("D", p.CreatedDate, DateTime.Now)
不行,他还是报无法转换为存储表达式的异常,其实问题应该是出在ef生成sql语句的过程中,它不会管前面是否有HasValue的,而是将HasValue和DateDiff都转成sql语句,但我网上搜来搜去别人都是直接用EntityFunctions.DiffDays或者SqlFunctions.DateDiff就可以的,但在我这就报错。另外我用的是EF6.0会不会有版本问题?或者说还要引入一些什么dll?[/quote][/quote] 果然解决了,方法就是引用System.Data.Entity然后使用里面的DbFunctions.DiffDays()方法[/quote]
romanchaos 2014-06-26
  • 打赏
  • 举报
回复
引用 7 楼 save4me 的回复:
参考: LINQ + EntityFunctions in asp.net mvc
引用
However, Entity Framework 6 has removed the dependency on System.Data.Entity and has redefined all necessary classes for that purpose inside of the EntityFramework assembly. Meaning that, in the case of your console app, I am guessing that either by design (System.Data.Entity is not referenced) or by accident (System.Data.Entity is referenced but the EntityFunctions class is taken from EntityFramework.dll), the correct version of EntityFunctions (from EntityFramework.dll) is taken. If you are using any version of Entity Framework 6, make sure that you are using the EntityFunctions class that can be found in EntityFramework.dll, not the one in System.Data.Entity. Source code of EntityFunctions.cs in Entity Framework 6 Actually, if you use Entity Framework 6, I would recommend removing all and any references to System.Data.Entity - in order to avoid any future confusion and mistake.
因为Entity Framework 6把System.Data.Entity的依赖关系已经移除了,放到了EntityFramework.dll,他建议在Entity Framework 6把所有的System.Data.Entity的引用删除,避免歧义和错误。你也试一下。 [quote=引用 5 楼 romanchaos 的回复:] [quote=引用 4 楼 dongxinxi 的回复:] 在前面加个条件 && p.CreatedDate.HasValue (对应SQL语句的 is not null) && SqlFunctions.DateDiff("D", p.CreatedDate, DateTime.Now)
不行,他还是报无法转换为存储表达式的异常,其实问题应该是出在ef生成sql语句的过程中,它不会管前面是否有HasValue的,而是将HasValue和DateDiff都转成sql语句,但我网上搜来搜去别人都是直接用EntityFunctions.DiffDays或者SqlFunctions.DateDiff就可以的,但在我这就报错。另外我用的是EF6.0会不会有版本问题?或者说还要引入一些什么dll?[/quote][/quote] 果然解决了,方法就是引用System.Data.Entity然后使用里面的DbFunctions.DiffDays()方法
exception92 2014-06-25
  • 打赏
  • 举报
回复
date.value.Day??
save4me 2014-06-25
  • 打赏
  • 举报
回复
参考: LINQ + EntityFunctions in asp.net mvc
引用
However, Entity Framework 6 has removed the dependency on System.Data.Entity and has redefined all necessary classes for that purpose inside of the EntityFramework assembly. Meaning that, in the case of your console app, I am guessing that either by design (System.Data.Entity is not referenced) or by accident (System.Data.Entity is referenced but the EntityFunctions class is taken from EntityFramework.dll), the correct version of EntityFunctions (from EntityFramework.dll) is taken. If you are using any version of Entity Framework 6, make sure that you are using the EntityFunctions class that can be found in EntityFramework.dll, not the one in System.Data.Entity. Source code of EntityFunctions.cs in Entity Framework 6 Actually, if you use Entity Framework 6, I would recommend removing all and any references to System.Data.Entity - in order to avoid any future confusion and mistake.
因为Entity Framework 6把System.Data.Entity的依赖关系已经移除了,放到了EntityFramework.dll,他建议在Entity Framework 6把所有的System.Data.Entity的引用删除,避免歧义和错误。你也试一下。
引用 5 楼 romanchaos 的回复:
[quote=引用 4 楼 dongxinxi 的回复:] 在前面加个条件 && p.CreatedDate.HasValue (对应SQL语句的 is not null) && SqlFunctions.DateDiff("D", p.CreatedDate, DateTime.Now)
不行,他还是报无法转换为存储表达式的异常,其实问题应该是出在ef生成sql语句的过程中,它不会管前面是否有HasValue的,而是将HasValue和DateDiff都转成sql语句,但我网上搜来搜去别人都是直接用EntityFunctions.DiffDays或者SqlFunctions.DateDiff就可以的,但在我这就报错。另外我用的是EF6.0会不会有版本问题?或者说还要引入一些什么dll?[/quote]
  • 打赏
  • 举报
回复
使用sql语句进行查询吧。 你不会学了EF,就把直接的ado.net给忘记了吧。
romanchaos 2014-06-25
  • 打赏
  • 举报
回复
引用 4 楼 dongxinxi 的回复:
在前面加个条件 && p.CreatedDate.HasValue (对应SQL语句的 is not null) && SqlFunctions.DateDiff("D", p.CreatedDate, DateTime.Now)
不行,他还是报无法转换为存储表达式的异常,其实问题应该是出在ef生成sql语句的过程中,它不会管前面是否有HasValue的,而是将HasValue和DateDiff都转成sql语句,但我网上搜来搜去别人都是直接用EntityFunctions.DiffDays或者SqlFunctions.DateDiff就可以的,但在我这就报错。另外我用的是EF6.0会不会有版本问题?或者说还要引入一些什么dll?
  • 打赏
  • 举报
回复
在前面加个条件 && p.CreatedDate.HasValue (对应SQL语句的 is not null) && SqlFunctions.DateDiff("D", p.CreatedDate, DateTime.Now)
save4me 2014-06-25
  • 打赏
  • 举报
回复
参考: EntityFunctions 类在Linq可以使用

//Value1 和 dateValue2 之间的月数。
DiffMonths(p.CreatedDate, DateTime.Now)
//dateValue1 和 dateValue2 之间的天数。
DiffDays(p.CreatedDate, DateTime.Now)
或者System.Data.Entity.SqlServer.SqlFunctions. Entity Framework 6 SqlFunctions DateDiff not implemented in nuget package

p =>  SqlFunctions.DateDiff("D", p.CreatedDate, DateTime.Now)
romanchaos 2014-06-25
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
date.value.Day??
不,作为条件我要搜一个月之前的数据

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧