How do I convert a List(Of T) to an ObservableCollection(Of T) in VB.NET?

前端 未结 6 2058
暗喜
暗喜 2020-12-31 01:33

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

6条回答
  •  悲哀的现实
    2020-12-31 02:13

    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 ToObservableCollection(this IEnumerable coll)
    {
        var c = new ObservableCollection();
        foreach (var e in coll) c.Add(e);
        return c;
    }
    

    You could pass an collection to the ObservableCollection constructor:

    List myProds = ......
    ObservableCollection oc = new ObservableCollection(myProds);
    

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

提交回复
热议问题