将 IList 中的元素添加到 ObservableCollection
我有一个 ObservableCollection,我想将 IList 的内容设置为此。现在我可以创建集合的一个新实例......:
public ObservableCollection<Bar> obs = new ObservableCollection<Bar>();
public void Foo(IList<Bar> list)
{
obs = new ObservableCollection<Bar>(list);
}
但是我如何才能真正获取 IList 的内容并将其添加到我现有的 ObservableCollection 中?我是否必须循环所有元素,或者有更好的方法吗?
public void Foo(IList<Bar> list)
{
foreach (var elm in list)
obs.Add(elm);
}
I have an ObservableCollection, and I'd like to set the content of an IList to this one. Now I could just create a new instance of the collection..:
public ObservableCollection<Bar> obs = new ObservableCollection<Bar>();
public void Foo(IList<Bar> list)
{
obs = new ObservableCollection<Bar>(list);
}
But how can I actually take the content of the IList and add it to my existing ObservableCollection? Do I have to loop over all elements, or is there a better way?
public void Foo(IList<Bar> list)
{
foreach (var elm in list)
obs.Add(elm);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然可能有一些“更好”的方法,其中涉及使用第三方依赖项、一些低级操作等。简单地循环集合就可以了并且可以工作。我建议这样做。简而言之,不,实际上没有“更好”的方法。
这是这个答案的旧版本,这是对 LINQ 的滥用(可能会迭代集合两次只是为了节省一两行代码)。我想完全删除答案,但我不能,因为它是已接受的答案。
你可以使用
or 作为扩展方法,
While there may be some "better" way which would involve using third party dependencies, some low level manipulation, etc. Simply looping over the collection is fine and works. I recommend doing that. In short—no, there is, effectively, no "better" way.
This is the old version of this answer, which is a misuse of LINQ (potentially iterating the collection twice just to save a line or two of code). I wanted to delete the answer entirely, but I can't, since it's the accepted answer.
You could do
or as an extension method,
如果您使用 C#3+ 来帮助您,您可以编写自己的扩展方法。该代码经过了一些基本测试以确保其有效:
You could write your own extension method if you are using C#3+ to help you with that. This code has had some basic testing to ensure that it works:
循环是唯一的方法,因为
ObservableCollection
没有等效的AddRange
方法。Looping is the only way, since there is no
AddRange
equivalent forObservableCollection
.这是
ObservableCollection
的后代,用于添加消息高效AddRange
以及单元测试:ObservableCollection 不支持 AddRange 方法,因此我会收到添加的每个项目的通知,除了 INotifyCollectionChanging 之外?
Here is an descendant to
ObservableCollection<T>
to add a message efficientAddRange
, plus unit tests:ObservableCollection Doesn't support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?
有一个库可以解决这个问题。它包含一个可以包装 List 的 ObservableList。它可以通过以下方式使用:
https://github.com/gsonnenf/Gstc.Collections .ObservableLists
There is a library that solves this problem. It contains an ObservableList that can wrap a List. It can be used in the following way:
https://github.com/gsonnenf/Gstc.Collections.ObservableLists
如果你确实想实例化一个可观察集合,并且想将一个新范围添加到可观察集合中,你可以按照我尝试过的以下方法:
通过这种方式,你可以将范围添加到 ObservableCollection 中,而无需循环。
If you do want to instantiate an observable collection and want to add a new range into Observable collection you can follow the following method I have tried:
in this way you can add the range into ObservableCollection without looping.