views:

6119

answers:

2

Is there a way to do this without iterating through the List and adding the items to the ObservableCollection?

+10  A: 

No, there is no way to directly convert the list to an observable collection. You must add each item to the collection. However, below is a shortcut to allow the framework to enumerate the values and add them for you.

Dim list as new List(of string)
...some stuff to fill the list...
Dim observable as new ObservableCollection(of string)(list)
Nescio
+4  A: 

hi, I'm late but I want to share this interesting piece for converting a list into a ObservableCollection if you need a loop:

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;
}

You could pass an collection to the ObservableCollection constructor:

List<Product> myProds = ......
ObservableCollection<Product> oc = new ObservableCollection<Product>(myProds);

Now you have to translate these to VB.NET :)

Junior Mayhé
That's C# not VB.NET but +1 anyway for solving it for any IEnumerable (and also for being late!)
MarkJ
Cool joke! It is real subtle humor!!
RredCat