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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:54:30+00:00 2026-06-03T15:54:30+00:00

Ok, here’s my problem. I have database table BloodCenters , which have latitude and

  • 0

Ok, here’s my problem. I have database table BloodCenters, which have latitude and longitude field. I want to query those table by using Linq Lambda Syntax by retrieving the BloodCenters which are inside the given radius relative to some point.

I am using this code, but it still give me error (inside the Where predicate):

public static List<BloodCenter> GetAllNearby(int latitude, int longitude, int radius, int limit){
    List<BloodCenter> res = null;
    res = new DBEntities().BloodCenters.Where(b =>
        Util.distance(latitude, longitude, b.Latitude, b.Longitude, "K") <= radius).Take(limit);
    return res;
}

Util.distance is a function returning a distance between 2 point of latitude and longitude.
Edited
So, I can’t put my own function inside the Where predicate. But can I compute the distance using SQL Syntax? Here’s my Util.Distance implementation:

public static double distance(double lat1, double longi1, double lat2, double longi2, string unit){
    var theta = longi1 - longi2;
    var dist = Math.Sin(DegreeToRadian(lat1)) * Math.Sin(DegreeToRadian(lat2)) +
        Math.Cos(DegreeToRadian(lat1)) * Math.Cos(DegreeToRadian(lat2)) * Math.Cos(DegreeToRadian(theta));
    dist = Math.Acos(dist);
    dist = RadianToDegree(dist);
    var miles = dist * 60 * 1.1515;
    unit = unit.ToUpper();
    if (unit.Equals("K"))
        return miles * 1.609344;
    else if (unit.Equals("N"))
        return miles * 0.8684;
    else
        return miles;
}
  • 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-03T15:54:32+00:00Added an answer on June 3, 2026 at 3:54 pm

    That will not work because Util.distance is implemented in your C# code and the query surface has no idea how that would be translated to SQL (or even if that would be possible).

    An immediate workaround is to fetch all records from the database and apply the predicate to instances, like this:

    res = new DBEntities().BloodCenters.ToList()
              .Where(b => Util.distance(latitude, longitude, b.Latitude, b.Longitude, "K") <= radius)
              .Take(limit);
    return res;
    

    Of course fetching all the records isn’t the best idea either, but sometimes there’s simply no other way.

    What you could try is to inline the logic of Util.distance inside the Where predicate. If the logic can be expressed using only “simple” operations (like basic math) the query surface would be able to translate it to SQL and everything would go smoothly.

    Here is a list of all methods that can be automatically translated to SQL.

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

Sidebar

Related Questions

Here is a complete example. I want to forbid using A::set from objects casted
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
Here is my problem : I have a post controller with the action create.
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here's my situation. I have a DotNetNuke application. I want to link to an
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote

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.