LinqToNhibernate中的DateTime陷阱

news/2024/7/3 13:51:47

能用LinqToNhibernate还是很爽的~~能获得编译时类型检查就不说了,就连where子句的复用都比用HQL爽快很多~~~

比如我有这种有时间限制的实体

     public   interface  ITerminableEntity
    {
        DateTime StartDate { 
get ; }
        DateTime
?  EndDate {  get ; }
    }

 

然后我给实现这种接口的实体统一实现时间判断的where子句

ExpandedBlockStart.gif 代码
     public   static   class  TerminableEntityDAOImpl
    {
        
public   static  Expression < Func < TEntity,  bool >>  DateAvailable < TEntity > ( this  CommonDAOImpl < TEntity >  dao, DateTime dt)
            
where  TEntity : ITerminableEntity, IEntity
        {
            
return  entity  =>  dt  >=  entity.StartDate  &&  ( ! entity.EndDate.HasValue  ||  entity.EndDate.Value  >  dt);
        }
    }

 

使用起来也是简单方便

ExpandedBlockStart.gif 代码
         public  IQueryable < ProgramEntity >  FindAllByCentre( string  entityName, Guid centreId)
        {
            
return  FindByCentreId(entityName, centreId).Where( this .DateAvailable(DateTime.Now));
        }

 

一运行却报错!NHibernate.QueryException: could not resolve property: Value

我确信我的实体类和mapping中都没有Value这个字段啊~~~问题肯定出在entity.EndDate.Value > dt这句上,把.Value去掉(亏得.net允许可空类型和非可空类型的比较),果然不报错了,但是显示结果却是空的,啥都没有!

看看生成出的SQL语句

ExpandedBlockStart.gif 代码
SELECT  ...  FROM  tblMFSServices this_  left   outer   join  tblCentres centre1_  on  this_.uidCentreKey = centre1_.uidCentreKey  WHERE  centre1_.uidCentreKey  =   @p0   and  (this_.dteEffectiveDate  <=   @p1   and  ( not  ( 1 = 1 or  this_.dteCeasedDate  >   @p2 ))

 

not (1=1) or this_.dteCeasedDate > @p2)。。。这啥玩意啊。。。真是雷死我了。。。

再改!entity.EndDate == null || entity.EndDate > dt(这次又亏得.net允许可空类型和null比,要是放以前还在使用Nhibernate.Nullables的时代真不知道咋整= =)

这次终于得到了正常的sql语句,显示结果也正常了。

顺便说一句Google这个问题的途中发现老外对LinqToNhibernate中的DateTime真是怨声载道啊,比如你想where someEntity.SomeDateTimeProperty.Date = new Date(2012, 12, 21)就不行~~LinqToNhibernate看来还有待完善啊~~

转载于:https://www.cnblogs.com/jiaxingseng/archive/2010/07/23/1783730.html


http://www.niftyadmin.cn/n/3613039.html

相关文章

Linux设备模型之input子系统详解(一)

------------------------------------------ 本文系本站原创,欢迎转载! 转载请注明出处:http://ericxiao.cublog.cn/ ------------------------------------------ 一&#xff1a;前言 在键盘驱动代码分析的笔记中&#xff0c;接触到了input子系统.键盘驱动&#xff0c;键盘驱…

创建你的第一个游戏Pong——挑战:改进您的Pong游戏

挑战&#xff1a;改进您的Pong游戏 现在轮到您来对游戏进行一些改进了&#xff1b;也许您能换一换图片或声音&#xff0c;或者调整游戏的玩法。如果您有Gamepad控制器&#xff0c;您还可以试着给它添加Rumble支持。而且如果您阅读本书的话&#xff0c;的确应该有一个Gamepad&am…

定义silverlight报表样式-Styling a Silverlight Chart

定义silverlight报表样式-Styling a Silverlight Chart A article on how to style a standard Silverlight chart to look like the Google Analytics chart.Download source code - 11.1 KBIntroductionI love Google Analytics! I use it to monitor every site I own… Goo…

linux input subsystem 架构分析

主要数据结构 struct input_dev { void *private; const char *name; const char *phys; const char *uniq; struct input_id id; unsigned long evbit[NBITS(EV_MAX)]; unsigned long keybit[NBITS(KEY_MAX)]; unsigned long relbit[NBITS(REL_MAX)]; unsigned long absbit[N…

HDU 2020,2021,2024,2028,2029,2030

//Made by syx //Time 2010年7月29日 09:55:28 // //2020 绝对值排序 //2021 发工资咯&#xff1a;&#xff09; //2024 C语言合法标识符 //2028 Lowest Common Multiple Plus //2029 Palindromes_easy version 回文串 //2030 汉字统计 /*//2030 汉字统计 #include <iostrea…

linux内存寻址

本章节介绍linux寻址技术&#xff0c;详细描述80x86微处理器怎样进行芯片级的内存寻址&#xff0c;linux又是如何寻址硬件的。 1. linux内存地址 80x86微处理器下主要有三种不同的地址&#xff1a;逻辑地址&#xff0c;线性地址&#xff0c;物理地址。 逻辑地址&#xff1a; 主…

Linux Input Device 介紹: APIs

Linux Input Device 介紹: APIs jollen 發表於 April 8, 2009 12:18 PM Linux 的 Input Device 是重要的一個 subsystem&#xff0c;在進行實例介紹前&#xff0c;先大略了解一下相關的 API。 Linux Input Device &#xfeff;input.c是Linux的”input”驅動程式&#xff0c;主…

Android 如何破解兼容性困局

最新的消息表明&#xff0c;Android 手机的销售量超越 iPhone&#xff0c;虽然在整体市场占有率上&#xff0c;仍然不及竞争对手&#xff0c;但是却已经初现王者风范&#xff0c;一些文章也预测 Android 最终会稳坐智能手机第一把交椅。Android 的确是十分有潜力的&#xff0c;…