title: NHibernate: Create criteria from subquery
body: <p>Have SQL:</p>
  
  <pre><code>select COUNT(1) from
  (SELECT TOP 10 Status
  FROM SESSIONS
  where SERVER_ID = 2569
  ORDER by ID desc) as s
  where s.STATUS = 32768
  </code></pre>
  
  <p>How do I create Criteria in NHiberbate? Language C#.</p>
  
  <pre><code>        var subQuery = DetachedCriteria.For(typeof(Session))
              .SetProjection(Property.ForName("Status"))
              .Add(Restrictions.Eq("Server", server))
              .AddOrder(Order.Desc("Id"))
              .SetMaxResults(10);
          var query = DetachedCriteria.For(typeof(Session))
              .Add(Subqueries.Exists(subQuery))
              .Add(Restrictions.Eq("Status.Id", valueFailStatus))
              .SetProjection(Projections.RowCount());
          using (var session = NHibernateHelper.OpenSession())
              return (int)query.GetExecutableCriteria(session)
              .UniqueResult();
  </code></pre>
  
  <p>But I get a sample of the entire table, and not one of those 10 elements.</p>