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

ToObservableCollection

Copies elements from IEnumerable<T> into ObservableCollection<T>. Handy for converting LINQ results into a list appropriate for WPF databinding.

Source

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
{
  ObservableCollection<T> list = new ObservableCollection<T>();

  foreach (T item in collection)
  {
    list.Add(item);
  }

  return list;
}

Example

ObservableCollection<object> filteredlist = (from object o in unfilteredlist select o).ToObservableCollection();

Author: Matt Nicholson

Submitted on: 17 aug. 2009

Language: C#

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

Views: 4952