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

ToObservableCollection<T>()

Convert a IEnumerable<T> to a ObservableCollection<T> and can be used in XAML (WPF, Silverlight, Windows Phone & Windows Store) projects

Source

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

namespace System.Linq
{
    public static class Enumerable
    {
        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList)
        {
            return enumerableList != null ? new ObservableCollection<T>(enumerableList) : null;
        }
    }
}

Example

var list = new List<Employee>()
var oc = new List<Employee>()
 
// add some employees
 
oc = list.OrderBy(emp => emp.Salary).ToObservableCollection();

Author: Farhan Ghumra

Submitted on: 10 jun 2013

Language: C#

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

Views: 3212