Silverlight / WPF - Convert VaR TO observable collection

Asked By gopal krish on 07-Jun-12 03:17 AM
hi..

ObservableCollection<MyTasks> visibleTasks = e.Result;
var filteredResults = from visibleTask in visibleTasks
                     
select visibleTask;

visibleTasks
= new ObservableCollection<MyTasks>(filteredResults);

it throws compile time error.

i want to convert var into  observable collection.

how to handle this..

regards
gopal.s
Neha Garg replied to gopal krish on 07-Jun-12 05:52 AM

Hi Gopal,


May be i am not getting your question perfectly (means why you want to do that), you can get the result easily...

 

Anyway see the below code it may helps you:


public static class CollectionExtensions

      {

        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> coll)

        {

          var c = new ObservableCollection<T>();

          foreach (var e in coll)

            c.Add(e);

          return c;

        }

      }


Hope it helps...