[英]Error : Sequence contains no elements
I got a error when i get lat and long using locationService.GetLatLongFromAddress
当我得到lat并长期使用
locationService.GetLatLongFromAddress
时出错
The error is : 错误是:
Sequence contains no elements
序列不包含任何元素
I have tried this code 我试过这段代码
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;
What is the problem? 问题是什么? How can i solve this?
我怎么解决这个问题?
You would typically get that if code uses .First()
or .Single()
on a sequence ( IEnumerable<T>
) that has (as the message suggests): no elements. 如果代码在序列(
IEnumerable<T>
)上使用.First()
或.Single()
,那么通常会得到它(如消息所示):没有元素。 Meaning: an empty sequence (not to be confused with a null
sequence). 含义:空序列(不要与
null
序列混淆)。 You don't show code that does that, so I can only assume this happens inside .GetLatLongFromAddress()
. 你没有显示那样做的代码,所以我只能假设这发生在
.GetLatLongFromAddress()
。 So it sounds like there is a bug, probably relating to the "not found" case, but in code that we can't see. 所以听起来有一个错误,可能与“未找到”的情况有关,但在代码中我们看不到。 Personally, I would expect the "not found" case to return a
null
, or to throw some explicit "not found" exception. 就个人而言,我希望“未找到”的情况下返回
null
,或者抛出一些明确的“未找到”异常。 If this bug is inside a library: tell the library authors about it. 如果这个bug在库中:告诉库作者有关它。 Or better: fix it, and submit a pull request (if you can).
或者更好:修复它,并提交拉取请求(如果可以的话)。
Edit: 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, that should be: 国际海事组织,应该是:
XDocument doc = XDocument.Load(string.Format(APIUrlLatLongFromAddress,
Uri.EscapeDataString(address)));
var els = doc.Descendants("result").Descendants("geometry")
.Descendants("location").FirstOrDefault();
if (null != els) {...}
One line code fix to send them... 一行代码修复发送它们......
I've merged in Mark Gravell's pull request for the GoogleLocationService and have pushed an updated Nuget package. 我已经合并了Mark Gravell对GoogleLocationService的拉取请求,并推出了更新的Nuget包。
https://www.nuget.org/packages/GoogleMaps.LocationServices/ https://www.nuget.org/packages/GoogleMaps.LocationServices/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.