views:

51

answers:

2

In Linq to NHibernate I'm trying to return businesses within a certain distance of a user. Here is what I have so far:

var query = from b in ActiveRecordLinq.AsQueryable<Business>()
                        where (3959 * Math.Acos(Math.Cos((Math.PI * coordinates.Latitude / 180)) * Math.Cos((Math.PI * b.Latitude / 180))
                            * Math.Cos((Math.PI * b.Longitude / 180) - (Math.PI * coordinates.Longitude / 180))
                            + Math.Sin((Math.PI * coordinates.Latitude / 180)) * Math.Sin((Math.PI * b.Latitude / 180)))) <= radiusInMiles
                        orderby b.Name ascending
                        select b;
            return query.ToList();

Unfortunately, it seems that the C# Math class isn't supported in Linq to NHibernate so it gives me the following error:

The method Cos is not implemented

How would I get around this?

Thanks! Justin

+1  A: 

You can't use arbitrary .Net functions in server-side Linq queries.

You can use server-side functions with HQL.

Diego Mijelshon
Linq to SQL supports the Math functions so I don't think they're arbitrary. Can you give an example of how I would do the above with HQL?
Justin
+1  A: 

You can do three things:

  1. Implement it and submit a patch to NHibernate so that everybody can use your implementation
  2. Create a failing unit test that isolates the problem and submit it to NHibernate Jira and wait until somebody else implements it.
  3. Do not use Linq, but plain old sql or mixed hql for this query.

You will make some people happy when you choose option 1.

Paco
Is HQL just the dynamic sql where you inject parameters by using : ? I have it working with this, I was just hoping to avoid this sort of approach.
Justin
HQL is not SQL. It's an object-oriented query language with a syntax very similar to SQL.
Diego Mijelshon