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

元气小坏坏 提交于 2019-12-20 09:59:19

问题


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

回答1:


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.




回答2:


Another method

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



回答3:


Another method:

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



回答4:


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

    }


来源:https://stackoverflow.com/questions/2489548/how-do-i-get-row-count-using-the-nhibernate-queryover-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!