ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToObservableCollection<T>()

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

Source

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

Author: Fons Sonnemans

Submitted on: 26 nov. 2008

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 16559