在 DataGrid 中显示大型集合
收藏物品数量:~100k 列中显示的字段数量:4-10
问题本身 - 集合是使用 EntityFramework 从数据库中获取的。在开发计算机上加载和具体化所有所需数据大约需要 10-12 秒。出现的另一件事是,同一个集合可以绑定到多个控件,因此,必须单独过滤它们(=不设置默认集合视图过滤器)。目前,我设置绑定如下:
Binding b = new Binding();
b.Source = new CollectionViewSource() { Source = MyLargeCollection }.View;
MyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, b);
创建一个新的 CollectionViewSource 大大增加了初始化所需的时间 - 几分钟(并且我怀疑它出于某种原因枚举了 100k 集合)。我的意思是,如果我只是设置:
b.Source = MyLargeCollection;
只需 10-12 秒即可从数据库加载和具体化数据。
问题 - 我的代码有问题吗?如果不是 - 将相同的大型集合绑定到不同的项目控件但具有不同的集合视图的正确方法是什么?
The amount of items in collection: ~100k
The amount of field displayed in columns: 4-10
The problem itself - the collection is taken from a database using EntityFramework. It takes about 10-12s on dev computers to load and materialize all the required data. Yet another thing that comes up is that the same collection can be bound to several controls, and, therefore, they must be filtered separately (= not setting the default collection view filters). Currently, I set the binding as follows:
Binding b = new Binding();
b.Source = new CollectionViewSource() { Source = MyLargeCollection }.View;
MyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, b);
Creating a new CollectionViewSource greatly increases the time it takes to initialize - several minutes (and I suspect that it is enumerating the 100k collection for some reason). I mean that if I just set:
b.Source = MyLargeCollection;
It will just take those 10-12 seconds to load and materialize data from the database.
The question - is there some problem with my code? If not - what would be the right approach for binding the same large collection to different items controls but with different collection views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用 Linq to Entities 加载具有指定过滤器的实体,以确保不会加载所有 10k 记录,因为没有用户对具有 10k 记录的网格感兴趣。
绑定到查询的示例:
通过这种方式,您只需加载控件所需的记录
Just use Linq to Entities to load the entities with its specified filter to make sure you don't load all 10k records because no user is interested in a grid with 10k records.
Example of binding to a query:
In this way you only load the records needed for your controls