ToObservableCollection for Linq and other purposes

Easily convert your Lists and enumerable collections and queries into an observable collection. I use this all the time for simplifying my Linq statements for inline calls.


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

namespace Seekford.Extensions
{
/// <summary>
/// Class for helping the conversion of types to observable collections, easier linq coding.
/// </summary>
public static  class ObservableExtension
{
/// <summary>
/// Exports the observable collection.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection">The collection.</param>
/// <returns></returns>
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
{
return new ObservableCollection<T>(collection);
}

/// <summary>
/// Exports the observable collection.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <returns></returns>
public static ObservableCollection<T> ToObservableCollection<T>(this List<T> list)
{
return new ObservableCollection<T>(list);
}


}
}

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *