NHibernate complex order query

Posted by manu08 on Stack Overflow See other posts from Stack Overflow or by manu08
Published on 2010-03-19T04:36:20Z Indexed on 2010/03/19 4:41 UTC
Read the original article Hit count: 295

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!

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping