0

This is my code:

   public IList<VotacaoPlenario> RetornarVotacao(int mesInicio, int anoInicio)
    {
        DetachedCriteria criteria = DetachedCriteria.For<VotacaoPlenario>();

        if (anoInicio > 0)
        { 
            criteria.Add(Expression.Eq("YEAR(Data)", anoInicio));

        }

        IList<VotacaoPlenario> votacao = criteria.GetExecutableCriteria(Session).List<VotacaoPlenario>();


        return votacao;
    }
}

In my table de field Data is Datime i'm need to compare with the variable anoInicio which is int How can i do that?

She-ra
  • 95
  • 1
  • 8

1 Answers1

2

The solution here could be to use SQL Projection:

var monthProjection = Projections
     .SqlProjection(" MONTH(Data) as month "  // the left side of the expression
                   , new[] {"month"}          // alias  
                   , new IType[] {NHibernateUtil.Int32}); // type is int

criteria.Add(Expression.Eq(monthProjection, anoInicio));

and the SQL generated would look like this

MONTH(Date) = @p1 //where p1 param is anaInicio
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Does not either the NHibernate.Transform. in my version is NHibernate.Util – She-ra May 23 '14 at 23:44
  • The *NHibernateUtil* live in a root namespace `NHibernate.NHibernateUtil`. The `Transformer` namespaces is not for Restrictions (as in our case) but for a final SELECT clause to Entity conversion *(mostly for projections)* – Radim Köhler May 24 '14 at 05:15