我有以下nhibernate映射
<class name="Customer">
<id name="Id">
<generator class="native"/>
</id>
<property name="Name"/>
<set name="Invoices" cascade="all" lazy="false">
<key column="CustomerId"/>
<one-to-many class="Invoice"/>
</set>
</class>
<class name="Invoice">
<id name="Id">
<generator class="native"/>
</id>
<property name="Status"/>
</class>
使用这些数据对象
public enum InvoiceStatus { Unpayed, Payed };
public class Customer
{
virtual public int Id { get; private set; }
virtual public string Name { get; set; }
virtual public ISet<Invoice> Invoices { get; set; }
}
public class Invoice
{
virtual public int Id { get; private set; }
virtual public InvoiceStatus Status { get; set; }
}
当我运行以下代码时
t = session.BeginTransaction();
session.SaveOrUpdate(new Customer
{
Name = "Customer1",
Invoices = new HashedSet<Invoice> { new Invoice { Status = InvoiceStatus.Payed } }
});
session.SaveOrUpdate(new Customer
{
Name = "Customer2",
Invoices = new HashedSet<Invoice> { new Invoice { Status = InvoiceStatus.Unpayed } }
});
session.SaveOrUpdate(new Customer
{
Name = "Customer3",
Invoices = new HashedSet<Invoice> {
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<Customer>().Where(c => c.Invoices.Any(i => i.Status == InvoiceStatus.Payed)).ToList().Count);
Console.WriteLine("{0} customers have unpayed invoices", session.Linq<Customer>().Where(c => c.Invoices.Any(i => i.Status == InvoiceStatus.Unpayed)).ToList().Count);
Console.WriteLine("Attempt 1: {0} customers have no invoices", session.Linq<Customer>().Where(c => c.Invoices.IsEmpty).ToList().Count);
Console.WriteLine("Attempt 2: {0} customers have no invoices", session.Linq<Customer>().Where(c => c.Invoices.Count == 0).ToList().Count);
Console.WriteLine("Attempt 3: {0} customers have no invoices", session.Linq<Customer>().Where(c => c.Invoices.Count() == 0).ToList().Count);
我得到了
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
最后三个结果不是我的预期,你能解释一下原因吗? 我怎样才能得到正确的结果?
答案 0 :(得分:0)
回答我自己的问题。当我在Where子句
中使用!c.Invoices.Any()时它可以工作Console.WriteLine("Attempt 4: {0} customers have no invoices", session.Linq<Customer>().Where(c => !c.Invoices.Any()).ToList().Count);