Monday, 2 November 2009

Linq GetEnumerator.reset and new MS Chart charting controls

So I thought I would try out the new Microsoft charting controls for ASP.Net and winforms...

Looks fantastic but when I came to do some runtime databinding to a simple linq collection (selecting an x and y value into an enumerable anonymous type...) it failed miserably with an obscure error message.

With a bit of snooping I found that the problem is that the chart control always calls .Reset() on its IEnumerable(Of T) datasource before iterating the collection. Unfortunately the System.Linq.Enumerable.Iterator(Of TSource) that Linq returns as a concrete IEnumerable throws a "NotImplementedException" in its implementation of IEnumerable.Reset() - good one MS! (Check it out in Reflector)

Simple answer - call .ToArray() on your Linq collection and hey presto your Linq query will work as a chart datasource.

UPDATE - Click here and vote for MS to fix this bug!!!

No comments: