开发者

NHibernate filter criteria - Hour of DateTime

I am having hard times convincing NHibernate (EF was able to do it) to filter based on the Hour property of a DateTime entity pr开发者_运维问答operty. My entity goes something like :

public class Invoice {
    // ...
    public DateTime Time { get; set; }
    // ...
}

I need to retrieve all the invoices that were "made" at a certain hour (let's say 22). A possible query could be :

from   i in s.Linq<Invoice>()
where  i.Type.Id == Invoice.Type.Local &&
       i.TimeOfRegister.Hour == 22
select i

However I am being thrown an exception stating that the property Hour of TimeOfRegister could not be resolved...

LE : The details of the exception : could not resolve property: TimeOfRegister.Hour of: MyCompany.Entities.Invoice


Sounds like NHibernate does not see the a DateTime as a component which has properties it can select on, it sees it as a single property. You probably have to use a function to obtain the Hour part of the DateTime.

In HQL for SqlServer2005: hour(i.TimeOfRegister)


Well I was advised by a colleague to take another route :

Define an int (or a byte) property on the entity (let's say "HourOfRegister") and then in the mapping class map it to a formula :

Map(a => a.HourOfRegister).Formula("DATEPART(HOUR, TimeOfRegister)");

Then I can use it in the query as I like it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜