在NHibernate中选择没有多对多关系的项目?

编程入门 行业动态 更新时间:2024-10-28 15:33:48
本文介绍了在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中选择没有多对多关系的项目?

本文发布于:2023-10-23 13:26:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有多   关系   项目   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!