nHibernate 3 - 带有日期时间的 QueryOver
我正在尝试编写一个查询来使用日期时间进行选择。 Year 作为 where 参数,但我从 nunit 收到此错误:
NHibernate.QueryException:无法解析属性:Register.Year of:Estudantino.Domain.Events
在类 Events 中,我有一个名为 Register 的属性作为 DateTime 类型。
public virtual DateTime Registada { get; set; }
这是返回错误的方法:
using (ISession session = NHibernateHelper.OpenSession())
{
return session.QueryOver<Evento>()
.Where(x => x.Register.Year == year)
.List();
}
变量year 的类型为int,已传递给该方法。
有人知道我做错了什么吗?我的数据库服务器是 SQL Server 2005 Express。
I'm trying to write a query to select using the DateTime. Year as a where parameter, but I'm receiving this error from nunit:
NHibernate.QueryException : could not resolve property: Register.Year of: Estudantino.Domain.Events
In class Events I've a property named Register as a DateTime type.
public virtual DateTime Registada { get; set; }
This is the method that is returning the error:
using (ISession session = NHibernateHelper.OpenSession())
{
return session.QueryOver<Evento>()
.Where(x => x.Register.Year == year)
.List();
}
The variable year is of type int, that is been passed to the method.
Does anyone have an idea of what i'm doing wrong? My database server is SQL Server 2005 Express.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
QueryOver 无法解析诸如
DateTime.Year
之类的内容。使用 LINQ 代替:
QueryOver does not resolve things like
DateTime.Year
.Use LINQ instead:
在QueryOver中,你可以直接说:
还有其他扩展方法:
MonthPart()
、DayPart()
等。In QueryOver, you can just say:
There is also other extension methods:
MonthPart()
,DayPart()
, etc.您可能需要在这里使用 HQL。基本上 NHibernate 并不真正知道如何处理
year
。我怀疑您需要使用RegisterFunction
来注册YEAR
函数。请完整阅读这篇文章,以充分理解它是什么你正在尝试做的事。
You may need to use HQL here instead. Basically NHibernate does not know really what to do with
year
. I suspect you need to use theRegisterFunction
to register aYEAR
function.Please read this article in its entirety to fully understand what it is you are trying to do.
MyYearFunction
that returns a year for a datewhere dbo.MyYearFunction(:date) = 12" ...
.Where(x => x.Register >= new DateTime(year, 1, 1) && x.Register <
新的日期时间(年 + 1, 1, 1))
.Where(x => x.Register >= new DateTime(year, 1, 1) && x.Register <
new DateTime(year + 1, 1, 1))