NHibernate complex order query
- by manu08
Here's my simplified domain
public class Notification
{
public Guid ID { get; set; }
public string Name { get; set; }
public IEnumerable<Location> Locations { get; set; }
}
public class Location
{
public Guid Id { get; set; }
public string Name { get; set; }
public decimal Longitude { get; set; }
public decimal Latitude { get; set; }
}
Notifications and Locations have a many-to-many relationship (defined in table LocationsOnNotification). What I'd like to do is query for the nearest 10 Notifications from a given longitude and latitude.
I've been trying to use ICriteria, but I'm not sure how to specify the ordering correctly:
return Session.CreateCriteria<Notification>()
.SetFirstResult(firstIndex)
.SetMaxResults(maxResults)
.AddOrder(Order.Asc( WHAT GOES HERE! ))
.List<Notification>();
What I've been thinking of so far is adding a formula property to the Location mapping; something like this:
<property name='Distance' formula='lots of geometry'/>
But I'm not sure if that can take in parameters (since I'd need to pass in the user's location to calculate the distance), plus I'm not sure how to specify it in the Order.Asc clause given that it's a property on a many-to-many association class.
Any ideas? Or perhaps I should take a different approach altogether.
Thanks in advance!