【问题标题】:How do I get row count using the NHibernate QueryOver api?如何使用 NHibernate QueryOver api 获取行数?
【发布时间】:2014-02-18 02:03:45
【问题描述】:

我正在使用 NHibernate 3.x 中的 QueryOver api。我想获得行数,但我使用的方法返回所有对象,然后获取集合的计数。有没有办法只返回行数的整数/长值?

我目前正在使用:

_session.QueryOver<MyObject>().Future().Count()

【问题讨论】:

    标签: nhibernate queryover rowcount


    【解决方案1】:

    在使用了 api 之后,就可以了:

    _session.QueryOver<MyObject>()
        .Select(Projections.RowCount())
        .FutureValue<int>()
        .Value
    

    如果您不想将其作为未来返回,则可以改为获取 SingleOrDefault&lt;int&gt;()

    【讨论】:

      【解决方案2】:

      另一种方法

      var count = Session.QueryOver<Employer>()
          .Where(x => x.EmployerIsActive)
          .RowCount();
      

      【讨论】:

        【解决方案3】:

        另一种方法:

        int employerCount = session
          .QueryOver<Employer>()
          .Where(x => x.EmployerIsActive) // some condition if needed
          .Select(Projections.Count<Employer>(x => x.EmployerId))
          .SingleOrDefault<int>();
        

        【讨论】:

          【解决方案4】:

          我是这样使用的:

          public int QuantidadeTitulosEmAtraso(Sacado s)
              {
                  TituloDesconto titulo = null;
                  Sacado sacado = null;
          
                  var titulos =
                          _session
                          .QueryOver<TituloDesconto>(() => titulo)
                          .JoinAlias(() => titulo.Sacado, () => sacado)
                          .Where(() => sacado.Id == s.Id)
                          .Where(() => titulo.Vencimento <= DateTime.Today)
                          .RowCount();
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-07-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多