Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting ObservableCollection to List?

How does one convert an ObservableCollection to a List of the held objects?

like image 439
Shawn Mclean Avatar asked Nov 01 '09 22:11

Shawn Mclean


People also ask

How do I cast ObservableCollection to list?

Given that ObservableCollection<T> implements IEnumerable<T> you can give it to the constructor of List<T> : List<T> myList = new List<T>(myObservableCollection); Where T is the type of the items in the collection.

How to convert List into ObservableCollection in c#?

You can directly change it to Observablecollection , just as follows: public static ObservableCollection<Model> list = new ObservableCollection<Model>() { new Model() { Text = "A" }, new Model() { Text = "B" }, new Model() { Text = "C" }, new Model() { Text = "D" } };

What is the difference between ObservableCollection and list?

The true difference is rather straightforward:ObservableCollection implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated. However, BindingList implements IBindingList.

How do you cast IEnumerable to ObservableCollection?

var myObservableCollection = new ObservableCollection<YourType>(myIEnumerable); This will make a shallow copy of the current IEnumerable and turn it in to a ObservableCollection.


2 Answers

Just need to add the namespace using System.Linq;

and use the method ToList() in the ObservableCollection object

like image 176
Jaider Avatar answered Sep 23 '22 23:09

Jaider


Depending on the type of object in the ObservableCollection... I'll assume it's an int for this example:

IEnumerable<int> obsCollection = (IEnumerable<int>)GetCollection(); var list = new List<int>(obsCollection); 
like image 39
Matthew Scharley Avatar answered Sep 24 '22 23:09

Matthew Scharley