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
Converting observable collection back to regular collection
提问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 int
for this example:
根据ObservableCollection
......中对象的类型,我假设它是int
这个例子的一个:
IEnumerable<int> obsCollection = (IEnumerable<int>)GetCollection();
var list = new List<int>(obsCollection);
回答by Robert Harvey
ObservableCollection
implements IList<T>
, so you should be able to use ToList()
on it.
ObservableCollection
实现IList<T>
,所以你应该可以使用ToList()
它。
回答by LJM
The Items property returns an IList. See http://msdn.microsoft.com/en-us/library/ms132435.aspx
Items 属性返回一个 IList。 请参阅 http://msdn.microsoft.com/en-us/library/ms132435.aspx
回答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 T
is 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 完成