Using current_timestamp in NHibernate QueryOver syntax

两盒软妹~` 提交于 2019-12-06 07:27:57

问题


Is it possible to request the usage of current_timestamp in any other nhibernate query methods except for SQL and HQL?

I was thinking of making some type of IUserType class of DbDateTime or something in order to solve the problem, but wouldn't know exactly how to accomplish it.

Anyone have any ideas?

example of what I want:

  session.QueryOver<User>()
         .Where(u => u.CreatedOn > current_timestamp)
         .List();

回答1:


You can use IProjection and SQL functions in the QueryOver API like this:

var result = session.QueryOver<User>()
             .Where(Restrictions.GtProperty(
                Projections.Property<User>(u => u.CreatedOn), 
                Projections.SqlFunction("current_timestamp", new NHibernate.Type.TimestampType())))
             .List();

This will result in the following SQL:

SELECT this_.... FROM [User] this_ WHERE this_.CreatedOn > sysdatetime() ; 



回答2:


This may not be applicable for your case, but this might work

session.QueryOver<User>()
     .Where(u => u.CreatedOn > DateTime.Now)
     .List();


来源:https://stackoverflow.com/questions/6155454/using-current-timestamp-in-nhibernate-queryover-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!