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

前端 未结 6 2059
暗喜
暗喜 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:17

    Even though I'm late, I wanna share a quick enhancement to Junior's answer: let the developer define the converter function used to convert observable collection objects from the source collection to the destination one.

    Like the following:

    public static ObservableCollection ToObservableCollection(this IEnumerable coll, Func converter)
        {
            var c = new ObservableCollection();
            foreach (var e in coll)
            {
                c.Add(converter(e));
            }
            return c;
        }
    

提交回复
热议问题