NHibernate标准查询-选择与连接实体不同的实体 [英] NHibernate Criteria Query - Select Distinct with Joined Entity

查看:76
本文介绍了NHibernate标准查询-选择与连接实体不同的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Person实体.每个人都有一个国家,我想选择所有在其中有人的不同国家.此条件查询返回所有不同的CountryID

I have a Person entity. Every person has a country, I want to select all the distinct countries that have people in them. This Criteria Query returns all the distinct CountryID's

criteria.SetProjection(Projections.Distinct(Projections.Property("Country")));

如何更改它以加入并获取国家/地区"实体,而不仅仅是ID?

How do I alter it to join and fetch the Country entity, not just the ID?

推荐答案

任何简单的方法都是使用子查询.也就是说,您可以在外部查询中选择与该内部查询匹配的国家/地区的整个国家/地区.

Any easy way would be to use a subquery. That is, you could select the whole country on the outer query where the country ID matches the inner query.

Subqueries.PropertyIn(
  "Country",
  innerDetachedCriteriaWhichFindsCountriesWithPeopleAndProjectsCountryId)

这篇关于NHibernate标准查询-选择与连接实体不同的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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