c#   linq   wpf   observablecollection


ToList(), ToArray() etc. are great little LINQ methods to convert IEnumerables into those respective types. Unfortunately LINQ doesn’t provide such a method to convert one into an ObservableCollection, but if you’ve had any experience with WPF you know that you can’t escape ObservableCollections. So, here’s a neat little Extension Method to do just that, so you don’t have to do the extra step every time.

public static class ExtensionMethods
{
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> _LinqResult)
    {
        return new ObservableCollection<T>(_LinqResult);
    }
}

Usage

var list = new List<int>() { 1, 2, 3 };
var obs = list.ToObservableCollection();