Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check for an empty set in Linq to NHibernate query
    primarykey
    data
    text
    <p>I have the following nhibernate mapping</p> <pre><code>&lt;class name="Customer"&gt; &lt;id name="Id"&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;property name="Name"/&gt; &lt;set name="Invoices" cascade="all" lazy="false"&gt; &lt;key column="CustomerId"/&gt; &lt;one-to-many class="Invoice"/&gt; &lt;/set&gt; &lt;/class&gt; &lt;class name="Invoice"&gt; &lt;id name="Id"&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;property name="Status"/&gt; &lt;/class&gt; </code></pre> <p>With these data objects</p> <pre><code>public enum InvoiceStatus { Unpayed, Payed }; public class Customer { virtual public int Id { get; private set; } virtual public string Name { get; set; } virtual public ISet&lt;Invoice&gt; Invoices { get; set; } } public class Invoice { virtual public int Id { get; private set; } virtual public InvoiceStatus Status { get; set; } } </code></pre> <p>When I run the following code</p> <pre><code>t = session.BeginTransaction(); session.SaveOrUpdate(new Customer { Name = "Customer1", Invoices = new HashedSet&lt;Invoice&gt; { new Invoice { Status = InvoiceStatus.Payed } } }); session.SaveOrUpdate(new Customer { Name = "Customer2", Invoices = new HashedSet&lt;Invoice&gt; { new Invoice { Status = InvoiceStatus.Unpayed } } }); session.SaveOrUpdate(new Customer { Name = "Customer3", Invoices = new HashedSet&lt;Invoice&gt; { new Invoice {Status = InvoiceStatus.Payed}, new Invoice {Status = InvoiceStatus.Unpayed} } }); session.SaveOrUpdate(new Customer { Name = "Customer4" }); t.Commit(); Console.WriteLine("{0} customers have payed invoices", session.Linq&lt;Customer&gt;().Where(c =&gt; c.Invoices.Any(i =&gt; i.Status == InvoiceStatus.Payed)).ToList().Count); Console.WriteLine("{0} customers have unpayed invoices", session.Linq&lt;Customer&gt;().Where(c =&gt; c.Invoices.Any(i =&gt; i.Status == InvoiceStatus.Unpayed)).ToList().Count); Console.WriteLine("Attempt 1: {0} customers have no invoices", session.Linq&lt;Customer&gt;().Where(c =&gt; c.Invoices.IsEmpty).ToList().Count); Console.WriteLine("Attempt 2: {0} customers have no invoices", session.Linq&lt;Customer&gt;().Where(c =&gt; c.Invoices.Count == 0).ToList().Count); Console.WriteLine("Attempt 3: {0} customers have no invoices", session.Linq&lt;Customer&gt;().Where(c =&gt; c.Invoices.Count() == 0).ToList().Count); </code></pre> <p>I get</p> <pre><code>2 customers have payed invoices 2 customers have unpayed invoices Attempt 1: 4 customers have no invoices Attempt 2: 3 customers have no invoices Attempt 3: 3 customers have no invoices </code></pre> <p>The last three results are not what I expected, can you explain why?<br> And how would I get the right results?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload