NHIbernate QueryOver分离标准和Any子句

时间:2012-04-04 11:44:08

标签: nhibernate

我在子查询中有以下内容失败:

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

我收到Any语句的错误:

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

如何更新QueryOver的上述限制?

2 个答案:

答案 0 :(得分:5)

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

什么类别的类别?如果这是一个实体:

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

如果这是基本属性:

productCategory.Category == parameter.Category

这是多对多的关系吗? (产品和类别)

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

答案 1 :(得分:1)

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