Query nearby locations in table in asp net core 2 and entity framework core 2


ASP.NET is a popular programming language used for building web applications. It provides a powerful framework for developing dynamic and interactive websites. In this article, we will explore how to query locations in a table using ASP.NET Core 2 and Entity Framework Core 2.

Setting up the

we dive into the code, let's make sure we have the necessary environment set up. Make sure you have the following installed:

  • Visual Studio 2017 or later
  • .NET Core SDK
  • Entity Framework Core 2

Creating the Database

First, let's create a database table to store the locations. We will use Entity Framework Core 2 to the database operations. Here's an example of how to define the Location model:

 class Location
    public int Id { get; set; }
    public string Name { get; set; }
    public double Latitude { get; set; }
    public double  { get; set; }

Next, we need to create a class to represent our database and define the DbSet for the Location model. Here's an example:

public class LocationDbContext : DbContext
    public DbSet Locations { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

Make sure to “your_connection_string_here” with the actual connection string for your database.

Querying Nearby Locations

Now that we have our database set up, let's move on to querying nearby locations. We will use the Haversine formula to calculate the distance between two locations on their latitude and longitude.

First, let's define a method that in a latitude and longitude and returns a list of nearby locations:

public List GetNearbyLocations(double latitude, double longitude, double radius)
    using (var context = new LocationDbContext())
        var nearbyLocations = context.Locations
            .Where(l => CalculateDistance(latitude, longitude, l.Latitude, l.Longitude) 

The CalculateDistance method is responsible for calculating the distance between two locations using the Haversine formula. Here's an example implementation:

private double CalculateDistance(double lat1, double lon1, double lat2, double lon2)
    var R = 6371; // Radius of the earth in km
    var dLat = ToRadians(lat2 - lat1);
    var dLon = ToRadians(lon2 - lon1);
    var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
    var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
    var distance = R * c; // Distance in km

    return distance;

private double ToRadians(double degrees)
    return degrees * Math.PI / 180;

Now, you can call the GetNearbyLocations method with the desired latitude, longitude, and radius to retrieve the nearby locations from the database.


In this article, we have explored how to query nearby locations in a table using ASP.NET Core 2 and Entity Framework Core 2. We have seen how to set up the environment, create the database, and implement the logic for querying nearby locations. By following these steps, you can easily incorporate location-based features into your ASP.NET web applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents