Convert a List<T> into an ObservableCollection<T>

raghu_3 picture raghu_3 · May 8, 2013 · Viewed 106.7k times · Source

I have a List<T> which is being populated from JSON. I need to convert it into an ObservableCollection<T> to bind it to my GridView.

Any suggestions?

Answer

Denis picture Denis · May 8, 2013

ObservableCollection < T > has a constructor overload which takes IEnumerable < T >

Example for a List of int:

ObservableCollection<int> myCollection = new ObservableCollection<int>(myList);

One more example for a List of ObjectA:

ObservableCollection<ObjectA> myCollection = new ObservableCollection<ObjectA>(myList as List<ObjectA>);