简体   繁体   English

如何使用Nhibernate Criteria比较datepart月?

[英]How to compare datepart Month using Nhibernate Criteria?

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? 在我的表de field中数据是Datime我需要与变量anoInicio进行比较,这是int我该怎么做?

The solution here could be to use SQL Projection: 这里的解决方案可能是使用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 并且生成的SQL看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM