ListCollectionView to List

Viewed 1796

I have a ListCollectionView that I would like to put into a List, but I'm unsure how to do this.
Below is the code that I have tried. It returns an error that ListCollectionView does not contain a definition for ToList().

   var repItems = (ListCollectionView)view;
   var listItems = repItems.ToList();

Can anyone show me how?

2 Answers

ListCollectionView implements non-generic IEnumerable so you can't call generic ToList extension method on it. You need to cast it first:

var repItems = (ListCollectionView)view;
listItems = repItems.Cast<object>().ToList();

You can do like this.Instead of using List use IList

var repItems = (ListCollectionView)view;
IList<object> list = repItems.SourceCollection as IList<object>;
Related