IList<T> to ObservableCollection<T>

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

Answers


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();

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"


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.


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


Silverlight 4 DOES have the ability to just 'new up' an ObservableCollection

Here's the shortened extension method possible in Silverlight 4.

public static class CollectionUtils
{
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> items)
    {
        return new ObservableCollection<T>(items);
    }
}

        IList<string> list = new List<string>();

        ObservableCollection<string> observable = 
            new ObservableCollection<string>(list.AsEnumerable<string>());

Dim taskList As ObservableCollection(Of v2_Customer) = New ObservableCollection(Of v2_Customer)
' Dim custID As Guid = (CType(V2_CustomerDataGrid.SelectedItem,  _
'         v2_Customer)).Cust_UUID
' Generate some task data and add it to the task list.
For index = 1 To 14
    taskList.Add(New v2_Customer() With _
                 {.Cust_UUID = custID, .Company_UUID, .City
                 })
Next

Dim taskListView As New PagedCollectionView(taskList)
Me.CustomerDataForm1.ItemsSource = taskListView

You can do this:

public class SomeTypeCollection: ObservableCollection<SomeType>
{
    public SomeTypeCollection() : base() { }
    public SomeTypeCollection(IEnumerable<SomeType> IEObj) : base(IEObj) { 
 }
}

public ConsumeIlist
{
    public SomeTypeCollection OSomeTypeCllt { get; set; }
    MyDbContext _dbCntx = new MyDbContext();
     public ConsumeIlist(){
          OSomeTypeCllt = new 
   SomeTypeCollection(_dbCntx.ComeTypeSQLTable);
     }
}

Need Your Help

scope of using declaration within a namespace

c++ namespaces scope using

Is it safe (and correct) in a C++ header file to use the using declaration within a namespace as follows:

Android: measureText() Return Pixels Based on Scaled Pixels

android paint text-size

So I use Paint's measureText() method to measure the width of a segment of text, but I wanted to measure text based on a certain text size. Say I wanted to get the width of a text segment that will...