title: Using LINQ find nearby places from database
body: <p>We want to receive list of nearby places from database using LINQ in ASP.NET 2012 and would like some feedback on our strategy.</p>
  
  <hr>
  
  <p>My table and fake data:</p>
  
  <pre><code>     PlaceId    Name       Latitude   Longitude 
         1          A          18.1        20.1
         2          B          18.2        20.2
         3          C          18.3        20.3
  </code></pre>
  
  <p>1) In our project the client current location (latitude and longitude) is taken as input</p>
  
  <p>2) At server side ,depending upon the client current location, we need to find nearby places from the database using LINQ</p>
  
  <p>Here's the code for SQL which I earlier used , but now we want to use LINQ. </p>
  
  <pre><code>SELECT  name, Latitude, Longitude , 
    ( 3959 * acos( cos( radians(?) )* cos( radians( Latitude) ) * cos( radians( Longitude ) - radians(?) ) 
   + sin( radians(?) ) * sin( radians( Latitude) ) ) ) AS distance 
  FROM TABLE_NAME 
  HAVING distance &lt; ? 
  ORDER BY distance LIMIT 0 , 20
  </code></pre>
  
  <p><strong>[But the question is how to write such an query in LINQ.]</strong></p>
  
  <p>My work on this:</p>
  
  <p>While searching for the solution, I came across this code</p>
  
  <pre><code>        var Value1 = 57.2957795130823D;
          var Value2 = 3958.75586574D;
  
          var searchWithin = 20;
  
      double latitude = ConversionHelper.SafeConvertToDoubleCultureInd(Latitude, 0),
            longitude = ConversionHelper.SafeConvertToDoubleCultureInd(Longitude, 0);
  
      var location = (from l in sdbml.Places
                      let temp = Math.Sin(Convert.ToDouble(l.Latitude) / Value1) *  Math.Sin(Convert.ToDouble(latitude) / Value1) +
                               Math.Cos(Convert.ToDouble(l.Latitude) / Value1) *
                               Math.Cos(Convert.ToDouble(latitude) / Value1) *
                               Math.Cos((Convert.ToDouble(longitude) / Value1) - (Convert.ToDouble(l.Longitude) / Value1))
                           let calMiles = (Value2 * Math.Acos(temp &gt; 1 ? 1 : (temp &lt; -1 ? -1 : temp)))
                           where (l.Latitude &gt; 0 &amp;&amp; l.Longitude &gt; 0)
                           orderby calMiles
                          select new location
                               {
                                      Name = l.name
                                  });
                          return location .ToList();
  </code></pre>
  
  <p>But the problem is ,how to reference ConversionHelper or under which namespace it comes.</p>
  
  <p><strong>All advice is appreciated.</strong></p>