将 IList 中的元素添加到 ObservableCollection

发布于 2024-08-19 15:29:00 字数 487 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤君无依 2024-08-26 15:29:00

但是我怎样才能真正获取 IList 的内容并将其添加到我的
现有的 ObservableCollection?我是否必须循环所有元素,或者
有更好的办法吗?

虽然可能有一些“更好”的方法,其中涉及使用第三方依赖项、一些低级操作等。简单地循环集合就可以了并且可以工作。我建议这样做。简而言之,不,实际上没有“更好”的方法。


这是这个答案的旧版本,这是对 LINQ 的滥用(可能会迭代集合两次只是为了节省一两行代码)。我想完全删除答案,但我不能,因为它是已接受的答案。

你可以使用

public void Foo(IList<Bar> list) =>
    list.ToList().ForEach(obs.Add);

or 作为扩展方法,

public static void AddRange<T>(
    this ObservableCollection<T> collection, IEnumerable<T> items) =>
    items.ToList().ForEach(collection.Add);

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?

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

public void Foo(IList<Bar> list) =>
    list.ToList().ForEach(obs.Add);

or as an extension method,

public static void AddRange<T>(
    this ObservableCollection<T> collection, IEnumerable<T> items) =>
    items.ToList().ForEach(collection.Add);
风铃鹿 2024-08-26 15:29:00

如果您使用 C#3+ 来帮助您,您可以编写自己的扩展方法。该代码经过了一些基本测试以确保其有效:

public static void AddRange<T>(this ObservableCollection<T> coll, IEnumerable<T> items)
{
    foreach (var item in items)
    {
        coll.Add(item);
    }
}

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:

public static void AddRange<T>(this ObservableCollection<T> coll, IEnumerable<T> items)
{
    foreach (var item in items)
    {
        coll.Add(item);
    }
}
辞旧 2024-08-26 15:29:00

循环是唯一的方法,因为 ObservableCollection 没有等效的 AddRange 方法。

Looping is the only way, since there is no AddRange equivalent for ObservableCollection.

酒几许 2024-08-26 15:29:00

这是 ObservableCollection 的后代,用于添加消息高效 AddRange 以及单元测试:

ObservableCollection 不支持 AddRange 方法,因此我会收到添加的每个项目的通知,除了 INotifyCollectionChanging 之外?

Here is an descendant to ObservableCollection<T> to add a message efficient AddRange, plus unit tests:

ObservableCollection Doesn't support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?

策马西风 2024-08-26 15:29:00

有一个库可以解决这个问题。它包含一个可以包装 List 的 ObservableList。它可以通过以下方式使用:

List<Bar> currentList = getMyList();
var obvList = new ObservableList<Bar>(currentList);

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:

List<Bar> currentList = getMyList();
var obvList = new ObservableList<Bar>(currentList);

https://github.com/gsonnenf/Gstc.Collections.ObservableLists

地狱即天堂 2024-08-26 15:29:00

如果你确实想实例化一个可观察集合,并且想将一个新范围添加到可观察集合中,你可以按照我尝试过的以下方法:

var list = new List<Utilities.T>();
            list.AddRange(order.ItemTransactions.ToShortTrans());
            list.AddRange(order.DealTransactions.ToShortTrans());
            ShortTransactions = new ObservableCollection<T>(list);

通过这种方式,你可以将范围添加到 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:

var list = new List<Utilities.T>();
            list.AddRange(order.ItemTransactions.ToShortTrans());
            list.AddRange(order.DealTransactions.ToShortTrans());
            ShortTransactions = new ObservableCollection<T>(list);

in this way you can add the range into ObservableCollection without looping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文