问题描述
在NHibernate中使用新的QueryOver
API,我需要执行以下操作:
Using the new QueryOver
API in NHibernate, I need to do something equivalent of:
select c.*
from Category c
where not exists (
select *
from CategoryProduct cp
where cp.CategoryID = c.Id
and cp.ProductID = 'DogFood'
)
换句话说:给我不包含狗食的所有类别".
In other words: "Give me all categories that doesn't contain dog food".
我最初的想法是:
IEnumerable<Category> FindCategoriesWithoutProduct(Product product)
{
return _session
.QueryOver<Category>()
.Where(c => c.Products.Contains(product))
.List();
}
但是,这会使NHibernate.Impl.ExpressionProcessor
在System.Collections.Generic.ICollection<T>.Contains()
上被无法识别的方法调用"炸毁.
However, this makes NHibernate.Impl.ExpressionProcessor
blow up with an "unrecognised method call" on System.Collections.Generic.ICollection<T>.Contains()
.
我认为还必须采用其他方法,可能涉及到ICriterion
,但是我在这里和Google上的搜索均未返回任何有用的信息.
I assume there must be some other way to do this, probably involving an ICriterion
, but my searches here and on Google have returned nothing useful.
推荐答案
我只是遇到了同样的问题,可能的解决方法是:
I'm just running in the same problem, and possible solution is:
Category aliasCategory = null;
CategoryProduct aliasCategoryProduct = null;
var qcp = QueryOver.Of<CategoryProduct>(() => aliasCategoryProduct)
.Where(() => aliasCategoryProduct.ProductID == "DogFood")
.Where(() => aliasCategory.Id == aliasCategoryProduct.CategoryID)
.DetachedCriteria;
return _session.QueryOver<Category>(() => aliasCategory)
.Where(Subqueries.NotExists(qcp));
它在我的类似条件下工作.
It works on my similar criteria.
这篇关于实施“不存在的地方".使用NHibernate QueryOver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!