开发者

Converting ObservableCollection to List?

How doe开发者_开发知识库s one convert an ObservableCollection to a List of the held objects?


Just need to add the namespace using System.Linq;

and use the method ToList() in the ObservableCollection object


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


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.


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

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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜