Showing posts with label extension method to Convert implicit type to observablecollection. Show all posts
Showing posts with label extension method to Convert implicit type to observablecollection. Show all posts

Tuesday, 7 June 2011

Reusable extension methods-c#

Convert implicit type to observablecollection OR Convert var to IEnumerable - Using extension method
public static class Extensions
{
       public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)
        {
            ObservableCollection<T> myColl = new ObservableCollection<T>();
            foreach (T element in source)
            {
                myColl .Add(element);
            }
            return myColl ;
        }
}


How to use:
Using Extensions;

var myImplicitVariable=from x in collection where somecondition select x;
ObservableCollection<T> myCollection=myImplicitVariable.ToObservableCollection();