1

Been trying to get the following working using nhibernate HQL or QueryOver. I am trying to get a sorted DISTINCT list of the entity organisation while sorting on joined tables and using paging. Entities are (shortened version):

Organisation

 OrganisationId
 Name
 StatusId
 SubstatusId

Address

 AddressId
 OrganisationId
 City
 CountryId

ListItem

 ListItemId
 Name

StatusId, SubstatusId and CountryId are foreign keys to ListItemId ListItemId is a list of user entered options. There can be more than one address for each organisation

To sort on Organisation.Address[x].Country.Name I need to include this in the select, but obviously then I dont get distinct organisations rather distinct organisations for a particular address.

I cant then get this into the nHibernate entity as it has the additional Country.Name field.

Also I cant run another distinct over the initial results as im using .SetFirstResult and .SetMaxResults to do SQL Paging. If I only want 10 rows, then I initially get 10 rows of distinct (organisation + country name). When i run another distinct over this will get reduced if an organisation has more than address.

Any ideas how to get a distinct paged list of organisations while sorting on Address.Name, Status.Name, SubStatus.Name


You could do it with 2 selects and a bit of linq:

  1. Do your projection selecting Organisation.Id, Address.Name, Status.Name, SubStatus.Name, ordering them appropriately and paging the results (well you just need to select Organisation.Id)

  2. Do another query to load the organisations by the Id's in the previous projection

  3. Combine the queries to order the list of organisations by their order in the first select

4

1 回答 1

0

您可以使用 2 个选择和一些 linq 来完成:

  1. 选择 Organisation.Id、Address.Name、Status.Name、SubStatus.Name 进行投影,对它们进行适当排序并分页结果(你只需要选择 Organisation.Id)

  2. 执行另一个查询以按上一个投影中的 Id 加载组织

  3. 结合查询以在第一个选择中按组织列表排序

于 2012-09-20T08:39:16.527 回答