Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get row count using the NHibernate QueryOver api?

I'm using the QueryOver api that is part of NHibernate 3.x. I would like to get a row count, but the method I'm using returns all objects and then gets the count of the collection. Is there a way to just return an integer/long value of the number of rows?

I'm currently using:

_session.QueryOver<MyObject>().Future().Count()
like image 271
Jim Geurts Avatar asked Mar 22 '10 01:03

Jim Geurts


4 Answers

After a bit of playing around with the api, this will do it:

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

If you don't want to return it as a future, you can just get the SingleOrDefault<int>() instead.

like image 136
Jim Geurts Avatar answered Nov 04 '22 23:11

Jim Geurts


Another method

var count = Session.QueryOver<Employer>()
    .Where(x => x.EmployerIsActive)
    .RowCount();
like image 36
Rafael Mueller Avatar answered Nov 04 '22 21:11

Rafael Mueller


Another method:

int employerCount = session
  .QueryOver<Employer>()
  .Where(x => x.EmployerIsActive) // some condition if needed
  .Select(Projections.Count<Employer>(x => x.EmployerId))
  .SingleOrDefault<int>();
like image 8
pero Avatar answered Nov 04 '22 21:11

pero


Im using like this:

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

    }
like image 7
Helder Gurgel Avatar answered Nov 04 '22 21:11

Helder Gurgel