Sending Generic.List collection back to WCF ObservableCollection

Found a good article and helped with a problem I was having, trying to send a list of collection back to a WCF service that was set to accept Observable Collections. 

Source: http://smehrozalam.wordpress.com/2009/01/18/ienumerabletoobservablecollection/

In WPF/Silverlight, binding UI objects such as DataGrid or ListBox to collections is typically done using an ObservableCollectioninstead of the generic List object. This way, our UI is automatically synchronized since the observable collection provides event notification to WPF data binding engine whenever items are added, removed, or when the whole list is refreshed. The LINQ extension methods that return a collection actually return IEnumerable<T>. The .NET framework for Silverlight provides built-in extension methods to convert IEnumerable<T> to List<T> and Array<T> but there’s no method available to convert the collection to ObservableCollection<T>(WPF developers can simply use this constructor overload) . So here’s one you may find useful:

  1. Public Static Class CollectionExtensions
  2. {
  3.     public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList)
  4.     {
  5.         if (enumerableList != null)
  6.         {
  7.             //create an emtpy observable collection object
  8.             var observableCollection = new ObservableCollection<T>();
  9.  
  10.             //loop through all the records and add to observable collection object
  11.             foreach (var item in enumerableList)
  12.                 observableCollection.Add(item);
  13.  
  14.             //return the populated observable collection
  15.             return observableCollection;
  16.         }
  17.         return null;
  18.     }
  19. }

Extension methods are very powerful and I am planning to post an example demonstrating their potential.

I’ve converted the above example to VB.Net version for you to copy.

Public Shared Function ToObservableCollection(Of T)(ByVal enumerableList As IEnumerable(Of T)) As ObservableCollection(Of T)
    If enumerableList IsNot Nothing Then
        'create an emtpy observable collection object
        Dim observableCollection As New ObservableCollection(Of T)()

        'loop through all the records and add to observable collection object
        For Each item As T In enumerableList
            observableCollection.Add(item)
        Next

        'return the populated observable collection
        Return observableCollection
    End If
    Return Nothing
End Function

Now you can call your List and send it back as a ObservableCollection

Comments (2) -

Better way of doing the above..

Public Static Class CollectionExtensions
{
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList)
    {
        if (enumerableList != null)
        {
            //create an emtpy observable collection object
            return new ObservableCollection<T>(enumerableList);
        }
        return null;
    }
}

Notice here that we have got rid of the loop.

Ahh this is much better, thank you.  I'll have to implement it in my project.

Comments are closed