在Criteria~Nhibernate C#中使用子查询的函数日期

时间:2017-06-06 15:07:58

标签: date nhibernate criteria

在SQL中,我有这个查询:

SELECT * 

FROM nfe_gerador

WHERE date(data_hora_emissao)

IN 

(

  SELECT date(n.data_hora_emissao) 

  FROM nfe_gerador n 

  ORDER BY data_hora_emissao DESC LIMIT 100

)

但是在Nhibernate中使用Criteria,我不知道如何使用函数日期。 这是我的代码,直到现在,但我没有获得像SQL(*)中的所有元素,我使用一些过滤器。

DetachedCriteria subQuery = DetachedCriteria.For<NFeGerador >("nfes");

            subQuery.CreateAlias("nfes.Empresa", "emp")
                .Add(Restrictions.Eq("emp.CNPJ", empresa.CNPJ))
                .SetProjection(Projections.ProjectionList()
                    .Add(Projections.Property("DataHoraEmissao"), "DataHoraEmissao"))
                        .SetMaxResults(quantidade)
                        .AddOrder(Order.Desc("DataHoraEmissao"));

            criterio.CreateAlias("nfe.Empresa", "emp")
                .Add(Restrictions.Eq("emp.CNPJ", empresa.CNPJ));
                criterio.SetProjection(Projections.ProjectionList()
                    .Add(Projections.Property("Id"), "Id")
                    .Add(Projections.Property("Serie"), "Serie")
                    .Add(Projections.Property("Numero"), "Numero")
                    .Add(Projections.Property("ChaveAcesso"), "ChaveAcesso")
                    .Add(Projections.Property("DataHoraEmissao"), "DataHoraEmissao")
                    .Add(Projections.Property("DataHoraAutorizacao"), "DataHoraAutorizacao")
                    .Add(Projections.Property("TipoEmissaoNFe"), "TipoEmissaoNFe")
                    .Add(Projections.Property("SituacaoNFeGerador"), "SituacaoNFeGerador")
                    .Add(Projections.Property("CpfCnpjDestinatario"), "CpfCnpjDestinatario")
                    .Add(Projections.Property("UFDestinatario"), "UFDestinatario"));

            criterio.Add(Subqueries.PropertyIn("DataHoraEmissao", subQuery));

            criterio.SetResultTransformer(Transformers.AliasToBean<NFeGerador>()).AddOrder(Order.Desc("DataHoraEmissao"));

如何使用函数日期获取子查询日期内的所有项目?

0 个答案:

没有答案