Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHIbernate QueryOver detached criteria and Any clause

Tags:

nhibernate

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?

like image 821
Samuel Goldenbaum Avatar asked Apr 04 '12 11:04

Samuel Goldenbaum


People also ask

What is QueryOver?

QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver.

What is DetachedCriteria in NHibernate?

The DetachedCriteria class lets you create a query outside the scope of a session, and then later execute it using some arbitrary ISession. A DetachedCriteria may also be used to express a sub-query.


1 Answers

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);
like image 83
Anton Avatar answered Nov 15 '22 08:11

Anton