C# 将可观察集合转换回常规集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1658643/
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-08-06 19:45:55  来源:igfitidea点击:

Converting observable collection back to regular collection

c#observablecollection

提问by Shawn Mclean

A function has a parameter of type object. I debugged it to see what type of datatype the other part of the program sent me. The base was of an ObservableCollection. How do i convert that to a list?

一个函数有一个对象类型的参数。我调试它以查看程序的另一部分发送给我的数据类型类型。基础是 ObservableCollection。我如何将其转换为列表?

采纳答案by Matthew Scharley

Depending on the type of object in the ObservableCollection... I'll assume it's an intfor this example:

根据ObservableCollection......中对象的类型,我假设它是int这个例子的一个:

IEnumerable<int> obsCollection = (IEnumerable<int>)GetCollection();
var list = new List<int>(obsCollection);

回答by Robert Harvey

ObservableCollectionimplements IList<T>, so you should be able to use ToList()on it.

ObservableCollection实现IList<T>,所以你应该可以使用ToList()它。

http://msdn.microsoft.com/en-us/library/bb342261.aspx

http://msdn.microsoft.com/en-us/library/bb342261.aspx

回答by LJM

回答by GraemeF

Given that ObservableCollection<T>implements IEnumerable<T>you can give it to the constructor of List<T>:

鉴于ObservableCollection<T>实现,IEnumerable<T>您可以将其提供给以下的构造函数List<T>

List<T> myList = new List<T>(myObservableCollection);

Where Tis the type of the items in the collection.

T集合中项目的类型在哪里。

回答by Jaider

Just need to add the namespace using System.Linq;

只需要添加命名空间 using System.Linq;

and use the method ToList()in the ObservableCollection object

并使用ToList()ObservableCollection 对象中的方法

回答by Ion Todirel

I think the issue here is that ObservableCollection might be modified on the fly when you try to convert it to a List or use as such, so you might need to use a watchdog timer of sorts until ObservableCollection finishes

我认为这里的问题是当您尝试将 ObservableCollection 转换为 List 或将其用作此类时,可能会动态修改 ObservableCollection,因此您可能需要使用各种看门狗计时器,直到 ObservableCollection 完成