我有一个带有集合 (InvoiceRows) 的类 (Invoice)。
类 -
public class Invoice
{
public string ID {get; set;}
public List<InvoiceRow> InvoiceRows {get; set;}
}
public class InvoiceRow
{
public string ID { get; set;}
public string InvoiceID { get; set;}
public int RowNumber { get; set;}
}
映射 -
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Domain.BusinessClasses.Invoice" table="Invoices" lazy="false">
<id name="ID">
<column name="ID"/>
<generator class="assigned"/>
</id>
<bag name="InvoiceRows" lazy="false" cascade="save-update" inverse="true" order-by="InvoiceRowNumber">
<key column="InvoiceID"/>
<one-to-many class="Domain.BusinessClasses.InvoiceRow" />
</bag>
</class>
<class name="Domain.BusinessClasses.InvoiceRow" table="InvoiceRows" lazy="false">
<id name="ID">
<column name="ID"/>
<generator class="assigned"/>
</id>
<property name="InvoiceID">
<column name="InvoiceID"/>
</property>
<property name="RowNumber">
<column name="RowNumber"/>
</property>
</class>
我想获取所有具有 InvoiceRow 且 RowNumber = 1 和 RowNumber = 2 的 Invoice 对象。最好使用 ICriterion API。
最佳答案
我设法根据 Genius 找到了答案回答。
var dCriteria1 = DetachedCriteria.For<InvoiceRow>("r")
.SetProjection(Projections.Property("r.RowNumber"))
.SetProjection(Projections.Property("r.InvoiceID")) // Must be last!!!!
.Add(Restrictions.Eq("r.RowNumber", 1));
var dCriteria2 = DetachedCriteria.For<InvoiceRow>("r")
.SetProjection(Projections.Property("r.RowNumber"))
.SetProjection(Projections.Property("r.InvoiceID")) // Must be last!!!!
.Add(Restrictions.Eq("r.RowNumber", 2));
var invoices = Session.CreateCriteria<Invoice>()
.Add(Subqueries.PropertyIn("ID", dCriteria1))
.Add(Subqueries.PropertyIn("ID", dCriteria2))
.List<Invoice>();
关于c# - NHibernate - 通过内部集合中的两个项目的标准获取类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8726022/