Construct DbGeography point from Latitude and Longitude doubles?

In short, no there isn’t.

SqlGeography has an appropriate method:

Microsoft.SqlServer.Types.SqlGeography.Point(latitude, longitude, srid);

… but you would then have to convert to DbGeography anyway. If you are interested in this, see a previous answer of mine on converting: DbGeography to SqlGeography (and back)

That said, I completely agree with Raphael Althaus in that you should create a static method to make your life easier:

public static DbGeography CreatePoint(double lat, double lon, int srid = 4326)
{
    string wkt = String.Format("POINT({0} {1})", lon, lat);

    return DbGeography.PointFromText(wkt, srid);
}

Then all usage can go through that method.

EDIT

@Korayem Made an excellent suggestion that I actually have done myself since originally answering the question. Most people use the SRID 4326 so we can make the static method easier to use by carrying that as the parameter’s default value.

Leave a Comment