Converting ObservableCollection to List?

C#Observablecollection

C# Problem Overview


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

C# Solutions


Solution 1 - C#

Just need to add the namespace using System.Linq;

and use the method ToList() in the ObservableCollection object

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

ObservableCollection implements IList<T>, so you should be able to use ToList() on it.

http://msdn.microsoft.com/en-us/library/bb342261.aspx

Solution 5 - C#

The Items property returns an IList. See http://msdn.microsoft.com/en-us/library/ms132435.aspx

Solution 6 - C#

I think the issue here is that ObservableCollection might be modified on the fly when you try to convert it to a List or use as such, so you might need to use a watchdog timer of sorts until ObservableCollection finishes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShawn McleanView Question on Stackoverflow
Solution 1 - C#JaiderView Answer on Stackoverflow
Solution 2 - C#Matthew ScharleyView Answer on Stackoverflow
Solution 3 - C#GraemeFView Answer on Stackoverflow
Solution 4 - C#Robert HarveyView Answer on Stackoverflow
Solution 5 - C#LJMView Answer on Stackoverflow
Solution 6 - C#user90843View Answer on Stackoverflow