¿Cómo obtengo el recuento de filas usando la api de NHibernate QueryOver?


Estoy usando la api QueryOver que es parte de NHibernate 3.x. Me gustaría obtener un recuento de filas, pero el método que estoy usando devuelve todos los objetos y luego obtiene el recuento de la colección. ¿Hay alguna manera de devolver un valor entero / largo del número de filas?

Actualmente estoy usando:

_session.QueryOver<MyObject>().Future().Count()
Author: Guillermo Gutiérrez, 2010-03-22

4 answers

Después de jugar un poco con la api, esto lo hará:

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

Si no desea devolverlo como un futuro, puede obtener el SingleOrDefault<int>() en su lugar.

 41
Author: Jim Geurts,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-15 20:35:30

Otro método

var count = Session.QueryOver<Employer>()
    .Where(x => x.EmployerIsActive)
    .RowCount();
 33
Author: Rafael Mueller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 11:09:14

Otro método:

int employerCount = session
  .QueryOver<Employer>()
  .Where(x => x.EmployerIsActive) // some condition if needed
  .Select(Projections.Count<Employer>(x => x.EmployerId))
  .SingleOrDefault<int>();
 8
Author: Petar Repac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-08 13:14:45

Estoy usando así:

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

    }
 7
Author: Helder Gurgel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-27 14:27:31