frosty
frosty

Reputation: 5370

nhibernate : how to query collection inside parent entity

I have the following method. This works fine if i remove the following line

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

The error message is

could not resolve property: Product.IsPopItem of: EStore.Domain.Model.ProductCategory

I'm confident the "Product.IsPopItem" is mapped correctly as i can call this property. Do i need to add a few criteria.

 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;
    }

Upvotes: 0

Views: 336

Answers (1)

isuruceanu
isuruceanu

Reputation: 1157

Yes you need to add an .CreateAleas

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

please change JoinType to your need, and use "product" alias instead of property name "Product"

so final should be something like:

.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;

Upvotes: 4

Related Questions