开发者

LinqToObject query Unable to cast object of type

开发者 https://www.devze.com 2023-03-24 10:55 出处:网络
Why is this开发者_如何学编程 query not working Private mapOverlays As New List(Of GMapOverlay)

Why is this开发者_如何学编程 query not working

Private mapOverlays As New List(Of GMapOverlay)

Dim mapOverlay = mapOverlays.Where(Function(x) x.Id = overlay.Name).Distinct()
DirectCast(mapOverlay,GMapOverlay).IsVisibile = False

I am getting the error

Unable to cast object of type 'd__7a`1[GMap.NET.WindowsForms.GMapOverlay]' to type 'GMap.NET.WindowsForms.GMapOverlay'.


Because mapOverlay is an enumerable of mapOverlays, not just one mapOverlay.

I think what you're trying to do is:

Dim mapOverlay = mapOverlays.Where(Function(x) x.Id = overlay.Name).Single()
mapOverlay.IsVisibile = False

If you move your mouse over mapOverlay, you'll see that the type that's being returned is effectively GMapOverlay and not IEnumerable(of GMapOverlay), because Single returns only one element. Distinct on the other hand can return more than one element, it just filters out duplicate values.

0

精彩评论

暂无评论...
验证码 换一张
取 消