How to convert IEnumerable to ObservableCollection?

How to convert IEnumerable to ObservableCollection?


As per the MSDN

var myObservableCollection = new ObservableCollection<YourType>(myIEnumerable);

This will make a shallow copy of the current IEnumerable and turn it in to a ObservableCollection.


  1. If you're working with non-generic IEnumerable you can do it this way:

    public ObservableCollection<object> Convert(IEnumerable original)
    {
        return new ObservableCollection<object>(original.Cast<object>());
    }
    
  2. If you're working with generic IEnumerable<T> you can do it this way:

    public ObservableCollection<T> Convert<T>(IEnumerable<T> original)
    {
        return new ObservableCollection<T>(original);
    }
    
  3. If you're working with non-generic IEnumerable but know the type of elements, you can do it this way:

    public ObservableCollection<T> Convert<T>(IEnumerable original)
    {
        return new ObservableCollection<T>(original.Cast<T>());
    }