LinqToObject query Unable to cast object of type
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.
精彩评论