Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:28:02+00:00 2026-06-05T05:28:02+00:00

I have an implementation of the Harversine formula in both c# and an example

  • 0

I have an implementation of the Harversine formula in both c# and an example in TSQL.
I’m unsure how is best to implement the formula on the server side so I’m able to use it within a Linq query.

Ideally, I’d just have my local formula linked to the function on the server. Thus avoiding the “no translation to sql” error and having everything nice and seamless.

Obviously any view on the problem is helpful.

I’m aware of the Geography types in SQL2008. However the code base i’m working against already has such a reliance on Linq to SQL I’d expect It’s more effort than it’s worth!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:28:04+00:00Added an answer on June 5, 2026 at 5:28 am

    why not going 100% SQL as it’s the best way to make the calculations, and simply get a table already filled up with the distances?

    from an existing answer

    CREATE FUNCTION dbo.udf_Haversine(@lat1 float, @long1 float, @lat2 float, @long2 float) RETURNS float 
      BEGIN
        DECLARE @dlon float, @dlat float, @rlat1 float, @rlat2 float, @rlong1 float, @rlong2 float, @a float, @c float, @R float, @d float, @DtoR float
    
        SELECT @DtoR = 0.017453293
        SELECT @R = 3937 --3976
    
        SELECT 
            @rlat1 = @lat1 * @DtoR,
            @rlong1 = @long1 * @DtoR,
            @rlat2 = @lat2 * @DtoR,
            @rlong2 = @long2 * @DtoR
    
        SELECT 
            @dlon = @rlong1 - @rlong2,
            @dlat = @rlat1 - @rlat2
    
        SELECT @a = power(sin(@dlat/2), 2) + cos(@rlat1) * cos(@rlat2) * power(sin(@dlon/2), 2)
        SELECT @c = 2 * atn2(sqrt(@a), sqrt(1-@a))
        SELECT @d = @R * @c
    
        RETURN @d 
      END
    

    and used like:

    var table = from r in db.VenuePostCodes 
                select new {
                    lat = r.Latitude,
                    lng = r.Longitude,
                    name = r.Name,
                    distance = db.udf_Haversine(
                                      r.Latitude,r.Longitude,
                                      r.Latitude,r.Longitude2)
                };
    

    but the best is always to have everything on SQL so your hosting server has less to do, simply ad a VIEW to your SQL and call that view, let’s imagine:

    SELECT 
       latitude, longitude, name, latitude1, longitude2, postcode, 
       udf_Haversine(latitude, longitude, latitude2, longitude2) AS distance 
    FROM 
       venuepostcodes
    ORDER BY 
       distance
    

    and use LINQ to call that view directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this implementation with fusion table layers where I try to use the
For example: I have an implementation (say doSomething()) in Base class. I want all
What strategy should I use if I have an implementation of std::fstream with 32-bit
I have IValueConverter implementation - IconExtractor . I use it this way: <Image Source={Binding
Background I have an implementation of an HTTP server in C#. Using ab I
I downloaded the JSON2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js and it does not have implementation for JSON2.stringify()
I have an implementation of lazyDataModel following this tutorial http://uaihebert.com/?p=1120 My code its a
I have an implementation of a DefaultTableCellRenderer. When someone selects a row in the
I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
I don't know either my implementation have bugs or StringBuilder is going wrong. From

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.