Unable to cast object of type ‘ Issue

Posted by Tim on December 22, 2009 in .NET Framework, Visual Basic |

I came across this error:

System.InvalidCastException: Unable to cast object of type ‘<TakeIterator>d__3a`1[Class]‘ to type ‘System.Collections.Generic.List`1[Class]‘.

This occured when trying to use the Take extension (list.Take(n)) from a generic list of objects.   The problem is when you use the Take extension, it returns a new sequence (

IEnumerable).

I got around this issue by using:

list.Take(8).ToList() which gets the new sequence and then converts back to the generic list.

References:

http://stackoverflow.com/questions/1431782/unable-to-cast-object

Tags: ,

Leave a Reply

Copyright © 2008-2012 Codelab Blog All rights reserved.
Desk Mess Mirrored version 1.9.1 theme from BuyNowShop.com.