How can I convert IEnumerable<object> to List<IFoo> where each object is an IFoo?
16,775
Solution 1
Using LINQ, you can use Cast
to cast the items, and use ToList
to get a list.
Try:
IEnumerable<object> someCollection; //Some enumerable of object.
var list = someCollection.Cast<IFoo>().ToList();
Solution 2
Try this:
enumerable.Cast<IFoo>().ToList();
Solution 3
someCollection.Cast<IFoo>().ToList()
Comments
-
Hcabnettek almost 2 years
How can I convert
IEnumerable<object>
toList<IFoo>
where each object is anIFoo
?I have an
IEnumerable<object>
,someCollection
, and each item insomeCollection
is anIFoo
instance. How can I convertsomeCollection
into aList<IFoo>
? Can I use convert or cast or something instead of looping through and building up a list?