How do I select the Count(*) of an nHibernate Subquery's results

后端 未结 6 1165
名媛妹妹
名媛妹妹 2021-01-04 21:52

I need to do the following for the purposes of paging a query in nHibernate:

Select count(*) from 
(Select e.ID,e.Name from Object as e where...)


        
相关标签:
6条回答
  • 2021-01-04 21:52

    I prefer,

        public IList GetOrders(int pageindex, int pagesize, out int total)
        {
                var results = session.CreateQuery().Add(session.CreateQuery("from Orders o").SetFirstResult(pageindex).SetMaxResults(pagesize));
    
                var wCriteriaCount = (ICriteria)results.Clone());
    
                wCriteriaCount.SetProjection(Projections.RowCount());
    
                total = Convert.ToInt32(wCriteriaCount.UniqueResult());
    
    
                return results.List();
        }
    
    0 讨论(0)
  • 2021-01-04 22:03

    If you just need e.Id,e.Name:

    select count(*) from Object where.....

    0 讨论(0)
  • 2021-01-04 22:06
    var session = GetSession();
    var criteria = session.CreateCriteria(typeof(Order))
                        .Add(Restrictions.Eq("Product", product))
                        .SetProjection(Projections.CountDistinct("Price"));
    return (int) criteria.UniqueResult();
    
    0 讨论(0)
  • 2021-01-04 22:10

    Solved My own question by modifying Geir-Tore's answer.....

     IList results = session.CreateMultiQuery()
            .Add(session.CreateQuery("from Orders o").SetFirstResult(pageindex).SetMaxResults(pagesize))
            .Add(session.CreateQuery("select count(distinct e.Id) from Orders o where..."))
            .List();
        return results;
    
    0 讨论(0)
  • 2021-01-04 22:10

    Here is a draft of how I do it:

    Query:

    public IList GetOrders(int pageindex, int pagesize)
    {
        IList results = session.CreateMultiQuery()
            .Add(session.CreateQuery("from Orders o").SetFirstResult(pageindex).SetMaxResults(pagesize))
            .Add(session.CreateQuery("select count(*) from Orders o"))
            .List();
        return results;
    }
    

    ObjectDataSource:

    [DataObjectMethod(DataObjectMethodType.Select)]
    public DataTable GetOrders(int startRowIndex, int maximumRows)
    {
        IList result = dao.GetOrders(startRowIndex, maximumRows);
        _count = Convert.ToInt32(((IList)result[1])[0]);
    
        return DataTableFromIList((IList)result[0]); //Basically creates a DataTable from the IList of Orders
    }
    
    0 讨论(0)
  • 2021-01-04 22:13

    NHibernate 3.0 allows Linq query.

    Try this

    int count = session.QueryOver<Orders>().RowCount();
    
    0 讨论(0)
提交回复
热议问题