Domanda

Sto cercando di scrivere una query per selezionare con il DateTime. Anno in cui come parametro, ma sto ricevendo questo errore da NUnit:

NHibernate.QueryException: non ha potuto risolvere proprietà: Register.Year di: Estudantino.Domain.Events

In classe Eventi Ho una proprietà denominata Registrazione come un tipo DateTime.

public virtual DateTime Registada { get; set; }

Questo è il metodo che restituisce l'errore:

  using (ISession session = NHibernateHelper.OpenSession())
        {
            return session.QueryOver<Evento>()
                .Where(x => x.Register.Year == year)
                .List();
        }

L'anno variabile è di tipo int , che è stato passato al metodo.

Qualcuno ha un'idea di quello che sto facendo male? Il mio server di database è SQL Server 2005 Express.

È stato utile?

Soluzione

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 the RegisterFunction to register a YEAR function.

Please read this article in its entirety to fully understand what it is you are trying to do.

  1. Create a custom dialect in code
  2. Create a function in SQL server called MyYearFunction that returns a year for a date
  3. Then use HQL (not sure if QueryOver can do this) to get the date where dbo.MyYearFunction(:date) = 12" ...

Altri suggerimenti

QueryOver does not resolve things like DateTime.Year.

Use LINQ instead:

return session.Query<Evento>()
              .Where(x => x.Register.Year == year)
              .ToList();

In QueryOver, you can just say:

dateTimeColumn.YearPart() == 1999

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

.Where(x => x.Register >= new DateTime(year, 1, 1) && x.Register < new DateTime(year + 1, 1, 1))

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top