【发布时间】:2022-04-29 00:51:17
【问题描述】:
我想从纬度和经度双精度中构造一个 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 方法。我不确定如何将我的纬度和经度加倍转换为格式正确的字节数组。
有没有比顶部的代码更简单的方法来构造一个经纬度双精度的 DbGeography 实例?
【问题讨论】:
-
你为什么不创建一个方法
public static DbGeography CreatePoint(double, double)来包装你的代码?它不会避免转换,但至少可以让你的代码更清晰...... -
我可能最终会做这样的事情。没有静态扩展方法太糟糕了;我希望能够将所有静态
DbGeography创建方法保留在一个类下。 -
并不是说这真的有帮助,但在 C# 6+ 版本中,您可以像这样使用字符串插值: DbGeography.FromText($"POINT({longitude} {latitude})");跨度>
标签: entity-framework geospatial