tags:

views:

2959

answers:

6

I have a method in a Silverlight app that currently returns an IList and I would like to find the cleanest way to turn this into an ObservableCollection so:

public IList<SomeType> GetIlist()
{
   //Process some stuff and return an IList<SomeType>;
}

public void ConsumeIlist()
{
   //SomeCollection is defined in the class as an ObservableCollection

   //Option 1
   //Doesn't work - SomeCollection is NULL 
   SomeCollection = GetIlist() as ObservableCollection

   //Option 2
   //Works, but feels less clean than a variation of the above
   IList<SomeType> myList = GetIlist
   foreach (SomeType currentItem in myList)
   {
      SomeCollection.Add(currentEntry);
   }
}

ObservableCollection doesn't have a constructor that will take an IList or IEnumerable as a parameter, so I can't simple new one up. Is there an alternative that looks more like option 1 that I'm missing, or am I just being too nit-picky here and option 2 really is a reasonable option.

Also, if option 2 is the only real option, is there a reason to use an IList over an IEnurerable if all I'm ever really going to do with it is iterate over the return value and add it to some other kind of collection?

Thanks in advance

+7  A: 

You could write a quick and dirty extension method to make it easy

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerable) {
  var col = new ObservableCollection<T>();
  foreach ( var cur in enumerable ) {
    col.Add(cur);
  }
  return col;
}

Now you can just write

return GetIlist().ToObservableCollection();
JaredPar
I had considered that, and probably should have said so. Mostly I want to be sure that there wasn't something buit-in that I was missing.
Steve Brouillard
@Steve unfortunately I dont' think you overlooked anything.
JaredPar
Jared, minor correction on your syntax - public static ObservableCollection<T> ToObservableCollection(this... should be public static ObservableCollection<T> ToObservableCollection<T>(this...I don't have enough rep to edit your post. Please fix for accuracy. Thanks.
Steve Brouillard
@Steve, thanks! fixed
JaredPar
Wouldn't it give problems when you add a new item to the list. It will loose track of the item since you are created a new ObservableCollection<T>.
azamsharp
@azamsharp: Indeed. 1) To* methods always make copies of their input 2) since the source doesn't provide INotifyCollectionChanged there ain't no way to detect changes to the original anyways.
David Schmitt
+2  A: 

Er...

ObservableCollection does have a constructor that will take an IEnumerable<T>, and IList<T> derives from IEnumerable<T>.

So you can "just new one up"

Randolpho
The documentation appears to be incorrect. I've verified this with reflector.exe
JaredPar
Is that perhaps the WPF version of ObservableCollection? I'm working in Silverlight. Editing to include that in the TEXT and not just the tag.
Steve Brouillard
@JaredPar: Are you saying that you used reflector to verify that the Silverlight version has an IEnumerable constructor?
Randolpho
@Steve Brouillard: I totally missed the Silverlight tag. My bad. I went and found the documentation, which does not include the IEnumerable constructor.
Randolpho
Silverlight version of the documentation with only default constructor: http://msdn.microsoft.com/en-us/library/ms668604(VS.95).aspx
Bearddo
Beardo - Thanks for the link.
Steve Brouillard
@Randolpho I used reflector to verify WPF version does not have that constructor. Almost 100% silverlight does not either
JaredPar
I does have a List<T> constructor, whihc he could have confused. But ultimately not relevant here.
Steve Brouillard
+1 for the grammar teasing.
John Gietzen
I'm soooo slow, I missed that completely. Not to mention I misread the post in the first place...ugghhh.
Steve Brouillard
@JaredPar: I'd verify either the .dll you're using or the version of it; I'm using WindowsBase.dll v3.0.0 (WPF version) and it certainly has the constructor in question; it not only compiles, but executes properly. I'm going to go test Silverlight now.
Randolpho
Ooh... just noticed something in the docs... the constructor was apparently added in .NET 3.0 SP2 / .NET 3.5 SP1. If you are using pre- service packs, that might be why you can't reflect on it.
Randolpho
A: 
        IList<string> list = new List<string>();

        ObservableCollection<string> observable = 
            new ObservableCollection<string>(list.AsEnumerable<string>());
Ragoczy
The Silverlight version of ObservableCollection only has the default constructor. Can't new one up and pass it anything to initialize it.
Steve Brouillard
+2  A: 

The extension method that JaredPar has given you is your best option in Silverlight. It gives you the ability to turn any IEnumerable into observable collection automatically simply by refering to the namespace, and reduces code duplication. There is nothing built in, unlike WPF, which offers the constructor option.

ib.

Ireney Berezniak
A: 

Not to reopen the thread but a constructor for ObservableCollection that takes IEnumerable has been added to silverlight 4

Brendan Cleary