Samuel Goldenbaum
Samuel Goldenbaum

Reputation: 18939

NHIbernate QueryOver detached criteria and Any clause

I have the following in subquery which is failing:

var subquery = QueryOver.Of<Product>()
                .Where(x => x.ProductCategories.Any(y => y.Categgory == parameter.Category));

I get an error for the Any statement:

Unrecognised method call: System.Linq.Enumerable:Boolean Any

How would I update the above restriction for QueryOver?

Upvotes: 5

Views: 5674

Answers (2)

Anton
Anton

Reputation: 1583

ProductCategory productCategory = null;
var subquery = QueryOver.Of<Product>()
  .JoinAlias(product => product.ProductCategories, () => productCategory)              
  .Where(() => productCategory.Category.Id == parameter.Category.Id);

What's type of Category? If this is an entity:

productCategory.Category.Id == parameter.Category.Id

If this is base property:

productCategory.Category == parameter.Category

Is it many-to-many relation? (Product and Category)

Category category = null;
var subquery = QueryOver.Of<Product>()
  .JoinAlias(product => product.Category, () => category)            
  .Where(() => category.Id == parameter.Category.Id);

Upvotes: 6

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

ProductCategory productCategory = null;
var subquery = QueryOver.Of<Product>()
  .JoinAlias(product => product.ProductCategories, () => productCategory)  
  .Where(Subqueries.WhereExists(CatExistsQuery())


private QueryOver<ProductCategory , ProductCategory > CatExistsQuery(<your type> parameter)
        {
            ProductCategory _innerCat = null;

            var query = (QueryOver<ProductCategory , ProductCategory >)Session
                        .QueryOver(() => _innerCat )
                        .Where(() => _productCategory.Id== _innerCat.Id)
                        .And (innerCat.Id == parameter.Category.Id)

            return query ;
        }

Upvotes: 1

Related Questions