如何在 VB.NET 中将 List(Of T) 转换为 ObservableCollection(Of T)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/243752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:53:04  来源:igfitidea点击:

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

vb.netcollectionslist

提问by Rob Sobers

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

有没有办法在不遍历 List 并将项目添加到 ObservableCollection 的情况下执行此操作?

回答by Nescio

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)

回答by Junior Mayhé

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

我迟到了,但如果您需要循环,我想分享这个有趣的文章,用于将列表转换为 ObservableCollection:

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:

您可以将集合传递给 ObservableCollection 构造函数:

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

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

现在您必须将这些转换为 VB.NET :)

回答by Joby Mavelikara

//Create an observable collection TObservable.

ObservableCollection (TObservable) =new ObservableCollection (TObservable)();

//Convert List items(OldListItems) to collection

OldListItems.ForEach(x => TObservable.Add(x));

回答by Jordan

to clarify what Junior is saying (with an added example if you're using LINQ that returns an IEnumerable):

澄清 Junior 所说的内容(如果您使用的是返回 IEnumerable 的 LINQ,则添加一个示例):

//Applications is an Observable Collection of Application in this example
List<Application> filteredApplications = 
      (Applications.Where( i => i.someBooleanDetail )).ToList();
Applications = new ObservableCollection<Application>( filteredApplications );

回答by trix

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.

即使我迟到了,我还是想分享一个对Junior答案的快速增强:让开发人员定义用于将可观察集合对象从源集合转换为目标集合的转换器函数。

Like the following:

像下面这样:

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

回答by Zin Min

ObservableCollection<yourobjectname> result = new ObservableCollection<yourobjectname>(yourobjectlist);