在NHibernate中选择没有多对多关系的项目? [英] Select items with no many-to-many relations in NHibernate?

查看:100
本文介绍了在NHibernate中选择没有多对多关系的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据库结构:

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.

我尝试了以下代码:

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.

我应该如何使用NHibernate执行此查询?

How should I perform this query with NHibernate?

推荐答案

尝试一下:

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

这篇关于在NHibernate中选择没有多对多关系的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆