Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nHibernate 3 - QueryOver with DateTime

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.

like image 578
Gui Avatar asked Dec 09 '22 11:12

Gui


2 Answers

QueryOver does not resolve things like DateTime.Year.

Use LINQ instead:

return session.Query<Evento>()
              .Where(x => x.Register.Year == year)
              .ToList();
like image 168
Diego Mijelshon Avatar answered Jan 29 '23 10:01

Diego Mijelshon


In QueryOver, you can just say:

dateTimeColumn.YearPart() == 1999

There is also other extension methods: MonthPart(), DayPart(), etc.

like image 27
AUSTX_RJL Avatar answered Jan 29 '23 11:01

AUSTX_RJL