How to compare datepart Month using Nhibernate Criteria?

对着背影说爱祢 提交于 2019-12-25 01:28:53

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/23818419/how-to-compare-datepart-month-using-nhibernate-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!