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 7817985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:25:12+00:00 2026-06-02T06:25:12+00:00

I am performing distance calculations between addresses. Specifically the distance between a User and

  • 0

I am performing distance calculations between addresses. Specifically the distance between a User and a particular location. I can build the LINQ queries that cause the database to calculate the distance and order the results by that distance. However, I can’t find a way to RETURN that distance in the Entities. A simplified version of the query is below:

    public IQueryable<Place> GetPlacesWithinRadiusOfUser(double userLongitude, double userLatitude, int miles )
    {

        return (from Place in this.ObjectContext.Places
                where RadiusOfEarth * Math.Acos((double)
                    (Math.Sin((double)Place.Addresses.First().Latitude / DegreesToRadians) * Math.Sin(userLatitude / DegreesToRadians))
                    +
                    (Math.Cos((double)Place.Addresses.First().Latitude / DegreesToRadians) * Math.Cos(userLatitude / DegreesToRadians)
                    * Math.Cos((userLongitude / DegreesToRadians) - ((double)Place.Addresses.First().Longitude / DegreesToRadians))
                    )
                    )
                    <= miles
                select Place);


    }

Essentially, the math from above I need to return as a Distance property on the Address Entity (which is child of Place). I have created the following partial class and the field does appear in the silverlight client.

public partial class Address
{
    //The Distance field is used to return a distance if a reference point is provided in a query
    [DataMember]
    public Nullable<decimal> Distance { get; set; }

}

So, the Distance property and calculation is dependent on the parameters of the GetPlacesWithinRadiusOfUser method.

Is there a way to get the parameters from the query to the Address Entity so that I can perform the same calculation and have it available in the Entity upon arriving at the client?

I can certainly create a client side routine that will do this calculation but this will get messy as I will have to continually inject that in every UI that needs it. I would much prefer to return it in the Entity so that I can just bind to it and display it (essentially I want a calculated field returned from SQL). If it were striaght SQL, this would be trivial but I am at a loss as to how to do this with RIA Services and the Entity Framework.

And thoughts?

  • 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-02T06:25:14+00:00Added an answer on June 2, 2026 at 6:25 am

    With Entity Framework, I was never able to find a way to actually do this (the way I wanted to). However, I found a pretty good solution. Steps are as follows:

    1. Created the query by issuing the distance calculation on the ObjectContext….then filtered a Place query by the returned values. All of that is done in one query call from the client. It doesn’t return the distance…but I handled that a different way.

    2. I then created extension methods on all of the relavent objects like so:

      public static class AddressExtensions
      {
      
      
      public static double CalculateDistanceTo(this Address address1, double longitude, double latitude)
      {
          if (address1 == null)
          {
              return 0;
          }
      
          return Global.CalculateDistance((double?)address1.Longitude, (double?)address1.Latitude, longitude, latitude);
      
      }
      
      public static double CalculateDistanceTo(this Address address1, Address address)
      {
          if (address1 == null || address == null)
          {
              return 0;
          }
      
          return Global.CalculateDistance((double?)address1.Longitude, (double?)address1.Latitude, (double?)address.Longitude, (double?)address.Latitude);
      
      }
      
      public static double CalculateDistanceTo(this Address address1, User user)
      {
          if (address1 == null)
          {
              return 0;
          }
      
          return address1.CalculateDistanceTo(user.Addresses.FirstOrDefault());
      
      }
      
      public static double CalculateDistanceTo(this Address address1, Place place)
      {
          if (address1 == null)
          {
              return 0;
          }
      
          return address1.CalculateDistanceTo(place.Addresses.FirstOrDefault());
      
      }
      }
      
    3. Then, anywhere I needed to get the distance, I could do something like User.DistanceTo(myAddress)

    This actually worked out a little better because I could then reuse the same extension methods no matter what the situation.

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

Sidebar

Related Questions

I'm looking for a Java library that is capable of performing spatial calculations on
I have a stored procedure in my database that calculates the distance between two
When performing these queries (separately): country:IN -or- country:IT -or- country:IS ... I get all
I'm dealing with physical entities, such as time, speed and distance, and performing simple
performing a GET in order to send data to an API before a user
After performing processing I want to calculate the percentage of white pixels between this
I am performing some simulations in python that generates 1000 results. Each result has
When performing a block like: <% @user.favoured_user.each do |user| %> <li><%= user.name %></li> <%
When performing UPDATE and INSERT queries using Zend_Db, I frequently need to set values
After performing some tests I noticed that printf is much faster than cout .

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.