nhibernate:如何查询父实体内部的集合

问题描述:

我有以下方法。如果我删除以下行这工作得很好nhibernate:如何查询父实体内部的集合

.Add(Restrictions.Eq("Product.IsPopItem", true)) 

的错误消息是

无法解析属性:的Product.IsPopItem:EStore.Domain.Model.ProductCategory

我确信“Product.IsPopItem”映射正确,因为我可以调用此属性。我是否需要添加一些标准。

public ICollection<ProductCategory> FindByCompanyIdAndCategoryIdAndIsPop(int companyId, int id) 
    { 
     var products = _session 
      .CreateCriteria(typeof(ProductCategory)) 
      .Add(Restrictions.Eq("CompanyId", companyId)) 
      .Add(Restrictions.Eq("CategoryId", id)) 
      .Add(Restrictions.Eq("Product.IsPopItem", true)) 
      .List<ProductCategory>(); 
     return products; 
    } 

是的,你需要添加一个.CreateAleas

.CreateAlias("Product", "product", JoinType.InnerJoin) 

请更改JoinType你的需要,并使用“产品”的别名,而不是属性名称“产品”

所以最后应该是像这样:

.CreateCriteria(typeof(ProductCategory)) 
     .CreateAlias("Product", "product", JoinType.InnerJoin) 
     .Add(Restrictions.Eq("CompanyId", companyId)) 
     .Add(Restrictions.Eq("CategoryId", id)) 
     .Add(Restrictions.Eq("product.IsPopItem", true)) 
     .List<ProductCategory>()); 
     return products; 
+0

打我,就像我开始键入:) – pythonandchips 2010-06-08 11:54:38

+0

:)对不起,只是帮助与同事同事 – isuruceanu 2010-06-08 11:59:59

+0

很好,很酷。 – frosty 2010-06-09 14:40:48