我想用纬度和经度双精度浮点数构建一个 DbGeography 点。
我知道我可以将我的双精度浮点数转换为字符串并使用 DbGeography.FromText
方法。
var latitude = 50.0d;
var longitude = 30.0d;
var pointString = string.Format(
"POINT({0} {1})",
longitude.ToString(),
latitude.ToString());
var point = DbGeography.FromText(pointString);
然而,将我的双精度浮点数转换为字符串,再让 DbGeography 将它们解析为双精度浮点数似乎是一种浪费。
我尝试直接构造 DbGeography,像这样:
var point = new DbGeography()
{
Latitude = 50,
Longitude = 30
};
但是纬度和经度属性是只读的。(这很有道理,因为DbGeography类处理的不仅仅是单个点)
DbGeography
类还提供了一个接受字节数组的FromBinary
方法。我不知道如何将我的纬度和经度double转换为正确格式的字节数组。
有没有比顶部代码更简单的方法来构建一个由纬度和经度double组成的DbGeography实例?
public static DbGeography CreatePoint(double, double)
来包装你的代码呢?这样做虽然不能避免转换,但至少可以让你的代码更清晰。 - Raphaël AlthausDbGeography
创建方法都放在一个类下面。 - jcarpenter2