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?
You can do like this.Instead of using List use IList
var repItems = (ListCollectionView)view;
IList<object> list = repItems.SourceCollection as IList<object>;
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 simply call repItems.SourceCollection
to get the underlying List. Since the ListCollectionViews data source is an IList
, you will always get a List back.
var sourceList = new List<int> {1,2,3,4,5,6,7,7,7,7,100};
var listView = new ListCollectionView(sourceList);
Console.WriteLine(listView.SourceCollection.GetType()); //System.Collections.Generic.List`1[System.Int32]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.