An unexpected exception which resulted in a gentle reminder of how Linq works...
Tonight I spent some time continuing my port of a Mobile Application I developed a few years ago over to UWP. As it was built in Xamarin.Forms , which also supports UWP, so far its been a fairly straight forward process. During testing tonight however I kept getting an uncaught exception every time I tapped a list item.
Specified argument was out of the range of valid values - Source Xamarin.Forms.Core
|My ListView ItemSource|
This is a perfect example of one, I'm assuming a fair bit here but I imagine there's a difference between how the various Xamarin.Form ListView implementations work. The iOS and Android implementations are quite happily lazily evaluating my Linq and manage the listview index's, where as the UWP implementation expects to have a materialised collection with all section indexes known,. Maybe all item indexes as well I'm not 100% sure.
By tweaking my code to materialise the WhereSelectListIterator before returning it, by calling ToList(), my UWP app is now happy again.
So there it is, it's sometimes easy to forget what Linq is actually doing and what it can mean for our applications, most of the time everything works as expected but its always worth ensuring you understand the basic concepts of it when things do go wrong, it can be the difference in spending 30 minutes looking at a bug and much longer...