ToObservableCollection<T>()

using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Databinding01 {
    public static class CollectionExtensions {

        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> coll) {
            var c = new ObservableCollection<T>();
            foreach (var e in coll)
                c.Add(e);
            return c;
        }
    }
}
Example:
var list = new ObservableCollection<Employee>()

// add some employees

list = list.OrderBy(emp => emp.Salary).ToObservableCollection();
            

Description

Convert a IEnumerable<T> to a ObservableCollection<T> and can be used in XAML (Silverlight and WPF) projects

Details

Double click on the code to select all.

 

;