ullevi83
ullevi83

Reputation: 199

ListCollectionView to List

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?

Upvotes: 2

Views: 2474

Answers (2)

Lukáš Koten
Lukáš Koten

Reputation: 1253

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();

Upvotes: 2

Sandeep P Pillai
Sandeep P Pillai

Reputation: 743

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

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

Upvotes: 0

Related Questions