c# - 错误 : Sequence contains no elements

标签 c# google-maps

当我使用 locationService.GetLatLongFromAddress 获取纬度和经度时出现错误

错误是:

Sequence contains no elements

我试过这段代码

var locationService = new GoogleLocationService();
var points = locationService.GetLatLongFromAddress("Ram Theatre Bus Stop, Arcot Road, Vadapalani, Chennai, Tamil Nadu");
mapDetail.Latitude = points.Latitude;
mapDetail.Longitude = points.Longitude;
mapDetail.CollegeAddressId = addressDetail[i].CollegeAddressId;

问题是什么?我该如何解决这个问题?

最佳答案

如果代码使用 .First(),您通常会得到它或 .Single()在具有(如消息所示)的序列 ( IEnumerable<T>) 上:没有元素。含义:一个空序列(不要与 null 序列混淆)。您没有显示这样做的代码,所以我只能假设这发生在 .GetLatLongFromAddress() 内部.所以听起来好像有一个错误,可能与“未找到”的情况有关,但在我们看不到的代码中。就个人而言,我希望“未找到”案例返回 null ,或者抛出一些明确的“未找到”异常。如果这个错误在库中:告诉库的作者。或者更好:修复它,然后提交拉取请求(如果可以的话)。

编辑:here we go :

XDocument doc = XDocument.Load(string.Format(APIUrlLatLongFromAddress,
    Uri.EscapeDataString(address)));
var els = doc.Descendants("result").Descendants("geometry")
    .Descendants("location").First();
if (null != els) {...}

IMO,应该是:

XDocument doc = XDocument.Load(string.Format(APIUrlLatLongFromAddress,
    Uri.EscapeDataString(address)));
var els = doc.Descendants("result").Descendants("geometry")
    .Descendants("location").FirstOrDefault();
if (null != els) {...}

一行代码修复发送它们...

关于c# - 错误 : Sequence contains no elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23109088/

相关文章:

c# - .Net MVC : need to display a list of users from AspNetUser table

javascript - ruby on Rails "Google is not defined"错误

javascript - 如何使用 javascript 结合地理定位和多个标记?

c# - 简化嵌套的 IF 语句

c# - T4MVC 不适用于 Url.Action()

C# winforms gridview 以编程方式绑定(bind)数据后排序

c# - 将 .NET 小数存储到 MySQL 中的最佳字段定义是什么?

android - MarkerClick 有效,但 InfoWindowClick 不打开 ViewModel

google-maps - 使用 React Native Google Maps Directions 包自动开始导航

android - 如何在 android studio 中设置 map api key ?