WPF的ICollectionView.filter与大型数据集 [英] WPF's ICollectionView.filter with large sets of data

查看:2489
本文介绍了WPF的ICollectionView.filter与大型数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,其中包含一个ListView有相当多的数据(10 000 100 000)行的WPF应用程序。用户可以应用各种过滤器的列表视图这使得过滤逻辑相当先进(慢)。现在,我的code的相关部分看起来是这样的:

I'm working on an wpf app which contains a listview with quite a lot of data (10 000 to 100 000) rows. The user can apply all sorts of filters to this listview making the filter logic quite advanced (and slow). For now, the relevant part of my code looks like this:

ICollectionView view = CollectionViewSource.GetDefaultView(hugeList.ItemsSource);
view.Filter = new Predicate<object>(FilterCallback);

private bool FilterCallback(object item)
{
  //Filter logic
}

但这个运行在UI线程和块时,过滤其中给出了一个非常糟糕的用户体验整个应用程序。所以,我给你所有的问题是:没有人知道一个'好'的方式在WPF过滤列表视图或者我应该过滤的基本的ObservableCollection 而不是

推荐答案

密切关注你的过滤功能。确保你没有做任何不必要的装箱/拆箱,你是不是在做它广泛的计算。你也应该支付您使用的是哪种类型的CollectionView的关注,有些是比别人快。从比亚对排序后

Pay close attention to your filter function. Make sure you aren't doing any unnecessary boxing/unboxing and you aren't doing extensive calculations in it. You should also pay attention to which kind of CollectionView you're using, some are faster than others. From Bea's post on sorting:


  • A 的CollectionView 是,如果你的源实现IEnumerable创建。如果源实现IEnumerable 的,你将无法排序或分组集(你只能将其过滤)。此外,香水不会是最好的,如果源代码中有大量的项目,或者如果您执行动态操作,如插入和删除。如果这是你的情况,你应该考虑让你的源代码实现更强的接口。 ICollection的是稍微好一点,因为它提供了一个Count属性。

  • A CollectionView is created if your source implements IEnumerable. If the source implements IEnumerable only, you will not be able to sort or group the collection (you can only filter it). Also, perf will not be the best if the source has a large number of items or if you perform dynamic operations such as insertions and deletions. If this is your scenario, you should consider having your source implement a stronger interface. ICollection is slightly better because it provides a Count property.

的ListCollectionView 是当源实现IList创建的视图类型。相比于IEnumerable和ICollection的,IList的执行多为大型或动态列表更好,因为它提供了一个索引,允许我们快速随机访问。此外,IList的允许排序,分组和过滤。但最好你的源集合来自的ObservableCollection,在数据绑定的眼中所有集合的母亲取得的,因为它提供了一些额外的好东西,如财产和收集更改通知。

ListCollectionView is the view type created when the source implements IList. Compared to IEnumerable and ICollection, IList performs much better for large or dynamic lists because it provides an indexer, allowing us quick random access. In addition, IList allows sorting, grouping and filtering. But ideally your source collection derives from ObservableCollection, the mother of all collections in the eyes of data binding, since it provides several extra goodies such as property and collection change notifications.

BindingListCollectionView 是当源集合实现IBindingList由阿瓦隆创建的视图类型。这是我们处理的ADO.NET方案的看法。它支持排序和分组,而不是传统的过滤。相反,它有过滤代表的数据视图(看到我对ADO.NET职位详细信息)的额外customFilter属性。

BindingListCollectionView is the type of view created by Avalon when the source collection implements IBindingList. This is the view we deal with in the ADO.NET scenario. It supports sorting and grouping, but not traditional filtering. Instead, it has an additional CustomFilter property that delegates filtering to the DataView (see my post on ADO.NET for more details).

您可以踢过滤到一个不同的线程作为@mihi说,但我已经使用CollectionViews对一个ObservableCollection拥有50000项的对象上60〜变量(列在数据库表)没有明显的滞后同时运行多个过滤器。

You can kick the filtering to a different thread as @mihi said but I have used CollectionViews to run multiple filters concurrently on an ObservableCollection with 50,000 items on an object with ~60 variables (columns in a database table) without noticeable lag.

有一件事情在你的过滤器的功能,我立刻注意到的是输入型对象,它可能意味着你正在做函数内的一个类型转换和可能不需要的。您还可以使用predicate不包括返回类型,这样可能需要的CollectionView的过滤方法中的某些类型转换或反射,可能你慢下来为好。

One thing I notice immediately in your filter function is the input is of type Object which likely means you're doing a type conversion within the function and may not need to. You also use Predicate which doesn't include a return type so that probably requires some type conversions or reflection within the CollectionView's filtering methods and might slow you down as well.

这篇关于WPF的ICollectionView.filter与大型数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆