<p>If i have a simple named query defined, the preforms a count function, on one column:</p>

<pre><code>  &lt;query name="Activity.GetAllMiles"&gt;
    &lt;![CDATA[
      select sum(Distance) from Activity
    ]]&gt;

  &lt;/query&gt;
</code></pre>

<p>How do I get the result of a sum or any query that dont return of one the mapped entities, with NHibernate using Either IQuery or ICriteria?</p>

<p>Here is my attempt (im unable to test it right now), would this work?</p>

<pre><code>    public decimal Find(String namedQuery)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            IQuery query = session.GetNamedQuery(namedQuery);


            return query.UniqueResult&lt;decimal&gt;();
        }
    }
</code></pre>

[{'answer_id': 67675, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 2, 'selected': False, 'text': '<p>As an indirect answer to your question, here is how I do it without a named query.</p>\n\n<pre><code>var session = GetSession();\nvar criteria = session.CreateCriteria(typeof(Order))\n            .Add(Restrictions.Eq("Product", product))\n            .SetProjection(Projections.CountDistinct("Price"));\nreturn (int) criteria.UniqueResult();\n</code></pre>\n'}, {'answer_id': 84348, 'author': 'Dan', 'author_id': 230, 'author_profile': 'https://Stackoverflow.com/users/230', 'pm_score': 3, 'selected': True, 'text': '<p>Sorry! I actually wanted a sum, not a count, which explains alot. Iv edited the post accordingly</p>\n\n<p>This works fine:</p>\n\n<pre><code>var criteria = session.CreateCriteria(typeof(Activity))\n                          .SetProjection(Projections.Sum("Distance"));\n   return (double)criteria.UniqueResult();\n</code></pre>\n\n<p>The named query approach still dies, "Errors in named queries: {Activity.GetAllMiles}":</p>\n\n<pre><code> using (ISession session = NHibernateHelper.OpenSession())\n            {\n                IQuery query = session.GetNamedQuery("Activity.GetAllMiles");\n\n\n                return query.UniqueResult&lt;double&gt;();\n            }\n</code></pre>\n'}, {'answer_id': 84424, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 0, 'selected': False, 'text': '<p>I think in your original example, you just need to to query.UniqueResult();  the count will return an integer.</p>\n'}]
['https://Stackoverflow.com/questions/65060', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/230/']