Select items with no many-to-many relations in NHibernate?

Deadly 提交于 2019-12-11 15:09:07

问题


I have this database structure:

Products
    ProductId

Categories
    CategoryId

ProductsInCategories
    ProductId
    CategoryId

I need to find all the products that are not in a category. Right now, I use this code:

var results = Session
    .CreateCriteria<Product>()
    .List<Product>()
    .Where(product=> !product.Categories.Any())
    .ToList();

So I return all the products in my database, then filter them. This is inefficient, I need a better method.

I tried this code:

var res = Session.QueryOver<Product>()
    .Left.JoinQueryOver(product=> product.Categhories)
    .Where(categories => !categories.Any())
    .TransformUsing(Transformers.DistinctRootEntity)
    .List();

But it didn't work at all. I tried some variations but that didn't work either.

How should I perform this query with NHibernate?


回答1:


Try this:

var res = Session.QueryOver<Product>()
    .WhereRestrictionOn(x => x.Categories).IsEmpty()
    .List();


来源:https://stackoverflow.com/questions/14977508/select-items-with-no-many-to-many-relations-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!