title: How can I include a nHibernate Criterion against a count of matching records
body: <p><strong>BACKGROUND:</strong></p>
  
  <ul>
  <li>I have Master and Detail tables, M and D;  </li>
  <li>M contains orders and D contains order detail with parts of various sizes (S, M, L, XL)</li>
  <li>A given order might contain any number of parts.</li>
  <li>95% of all orders contain at least one item of size S</li>
  <li>New requirement is that sizes can be added in the future, so hard coding the list for SMLXL no longer works</li>
  </ul>
  
  <p>I am looking for a nHibernate query that returns a list of all orders M that only contain parts with a specific size.</p>
  
  <p>In other words, return all order containing only parts of size S, and exclude all orders that contain a mixture of sizes.  </p>
  
  <p>I was using:</p>
  
  <pre><code>matching_orders.Add(
   Expression.Conjunction()
   .Add(Subqueries.WhereProperty&lt;Orders&gt;(o =&gt; o.OrderId).In(DetailQueryOver(S)))
   .Add(Subqueries.WhereProperty&lt;Orders&gt;(o =&gt; o.OrderId).NotIn(DetailQueryOver(M)))
   .Add(Subqueries.WhereProperty&lt;Orders&gt;(o =&gt; o.OrderId).NotIn(DetailQueryOver(L)))
   .Add(Subqueries.WhereProperty&lt;Orders&gt;(o =&gt; o.OrderId).NotIn(DetailQueryOver(XL)))
  </code></pre>
  
  <p>There must be a better way.  Something like "where count(DISTINCT SIZES) = 1"</p>
  
  <p>But I'm not sure how to implement this in nHibernate.</p>
  
  <p>Suggestions?</p>