简体   繁体   English

在可观察集合上使用 LINQ 而不会丢失事件订阅 C#

[英]Using LINQ on Observable Collection without losing Event subscriptions C#

I'm trying to use LINQ to sort a ObservableCollection, the problem is the LINQ's methods return a IOrderedEnumerable and there's not method to convert that type directly to ObservableCollection.我正在尝试使用 LINQ 对 ObservableCollection 进行排序,问题是 LINQ 的方法返回 IOrderedEnumerable 并且没有将该类型直接转换为 ObservableCollection 的方法。 After searching for a while I discovered a way do to this by using a constructor from the ObservableCollection class to create a new one using the IOrderedEnumerable:搜索了一段时间后,我发现了一种方法,通过使用 ObservableCollection 类中的构造函数来创建一个使用 IOrderedEnumerable 的新构造函数:

MyObservableCollection = new ObservableCollection<T>(someIOrderedEnumerable);

However this does not solve my problem because by using this constructor the previous EventHandler is erased so I lose the references to all methods previously subscribed to the CollectionChanged event and subscribing those methods again is not possible.但是,这并不能解决我的问题,因为通过使用此构造函数,先前的 EventHandler 被擦除,因此我丢失了对先前订阅 CollectionChanged 事件的所有方法的引用,并且无法再次订阅这些方法。

Currently what I'm doing is clearing the Observable Collection and then adding the objects that were returned by the LINQ method one by one to the Observable Collection, the problem is doing this fires the CollectionChanged event many times whenever I use LINQ to order it:目前我正在做的是清除 Observable 集合,然后将 LINQ 方法返回的对象一一添加到 Observable 集合中,问题是每当我使用 LINQ 对其进行排序时,都会多次触发 CollectionChanged 事件:

var iOrderedEnumerable = MyObservableCollection.SomeLINQMethod();
MyObservableCollection.Clear();
var pivotList = iOrderedEnumerable.ToList();
for (int i = 0; i < pivotList.Count; i++)
  {
      MyObservableCollection.Add(pivotList[i]);
  }

Main question is: How do I order a Observable Collection without losing the CollectionChanged event's subscriptions?主要问题是:如何在不丢失 CollectionChanged 事件订阅的情况下订购 Observable Collection?

I thought about storing the EventHandler and re-assign it after creating the new sorted Observable Collection, but haven't been able to implement this yet.我想过存储 EventHandler 并在创建新的排序的 Observable 集合后重新分配它,但还没有能够实现这一点。

Edit1: After tinkering for a while I figured a solution for LINQ methods that only sort collections, instead of clearing the Observable Collection and then add one by one i simply set the values instead(which does not fire the CollectionChanged Event): Edit1:经过一段时间的修补后,我想出了一个只对集合进行排序的 LINQ 方法的解决方案,而不是清除 Observable 集合,然后一个一个地添加我只是简单地设置值(它不会触发 CollectionChanged 事件):

var iOrderedEnumerable = MyObservableCollection.SomeLINQMethod();
var pivotList = iOrderedEnumerable.ToList();
for (int i = 0; i < pivotList.Count; i++)
    {
        MyObservableCollection[i] = pivotList[i];
    }

My library ObservableComputations can help you.我的库ObservableComputations可以帮助你。 Using that library your code will look like following:使用该库,您的代码将如下所示:

    ObsevableCollection<Item> orderedObsevableCollection = MyObservableCollection.Ordering(i => i.OrderKey);

ObsevableCollection will reflect all the changes in MyObservableCollection and OrderKey property of all items. ObsevableCollection 将反映所有项目的 MyObservableCollection 和 OrderKey 属性中的所有更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM