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:
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)
Do another query to load the organisations by the Id’s in the previous projection
Combine the queries to order the list of organisations by their order in the first select