4

I have a db contain customers longitude and latitude, I have a search form in which the user will enter log/lat and distance dropdown contain 50miles, 100miles,.... and when the user click search I want to write a linq query to get all customers from the db which are in this distance radius. How to make this using C# and linq?

Update:
I found this https://stackoverflow.com/a/1654365/20126 but this gives a square not radius

Community
  • 1
  • 1
Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301

2 Answers2

10

A little modification of my answer to a similar question:

// radius is the distance in meters
var center = new GeoCoordinate(latitude, longitude);
var result = customers.Select(x => new GeoCoordinate(x.Latitude, x.Longitude))
                      .Where(x => x.GetDistanceTo(center) < radius);

You need to add reference to System.Device.dll.

Community
  • 1
  • 1
Fung
  • 3,508
  • 2
  • 26
  • 33
  • 3
    As far as I can tell, GeoCoordinate.GetDistanceTo() can't be translated into SQL through L2E / EntityFramework – RJB Aug 06 '14 at 02:55
0

Using haversine formula

User will have current location, customer location and distance So using haversine formula calculate distance (d1) using current location and customer location Then compare calculated distance (d1) with required distance.

Logic is calculated distance <= required distance then it means current customer is in required distance else customer is outside the radial distance

Nitin Dominic
  • 2,659
  • 1
  • 20
  • 22