如何使用Nhibernate条件比较datepart月? [英] How to compare datepart Month using Nhibernate Criteria?

查看:122
本文介绍了如何使用Nhibernate条件比较datepart月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

   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;
    }
}

在我的表中,字段Data是Datime,我需要与变量anoInicio进行比较,该变量是int我该怎么做?

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?

推荐答案

这里的解决方案可能是使用SQL Projection:

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));

生成的SQL看起来像这样

and the SQL generated would look like this

MONTH(Date) = @p1 //where p1 param is anaInicio

这篇关于如何使用Nhibernate条件比较datepart月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆