Given a user's latitude and longitude, I want to retrieve other users around me from the database.

229 Views Asked by At

Possible Duplicate:
How to call user defined function in LINQ in my visual C# web service for my android application?

I am currently developing an application on Android that will make use of the user location. The database will keep all the user location by latitude and longitude. I know how to calculate the distance from two points and it is a bit complicated.

However, in my webservice written in visual C#, I can't write something like this:

from a in db.Location.Where(a => distanceBetweenTwoLocations(givenLat, givenLong, a.lat, a.longi)<500) select new {...}

because the linq wont let me call my own function. Can anyone suggest some way to achieve my goal? For simplicity, i want to find other users that is 500m around me. I have all the user locations in latitude and longitude in my database.

2

There are 2 best solutions below

0
On

There is an answer already, although it is in miles (1609.344 meters) instead of 500, but the calculation used is likely to fit your bill. It is using SQL, not LINQ, but you can easily convert this to a LINQ expression, especially if you have a tool like LINQPad.

0
On

Have a look at Nerddinner. http://nerddinnerbook.s3.amazonaws.com/Part11.htm

(use distancebetween function)

Create the relevant functions in your db.

then call them in your c#