使用 linq 对可观察集合进行排序 [英] Sorting an observable collection with linq

查看:29
本文介绍了使用 linq 对可观察集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可观察的集合,我使用 linq 对其进行排序.一切都很好,但我遇到的问题是如何对实际的可观察集合进行排序?相反,我最终得到了一些 IEnumerable 的东西,我最终清除了集合并重新添加了这些东西.这对性能不利.有没有人知道更好的方法来做到这一点?

I have an observable collection and I sort it using linq. Everything is great, but the problem I have is how do I sort the actual observable collection? Instead I just end up with some IEnumerable thing and I end up clearing the collection and adding the stuff back in. This can't be good for performance. Does anyone know of a better way to do this?

推荐答案

如果您使用的是 Silverlight 3.0,那么使用 CollectionViewSource 是最干净的方式.请参考以下示例:(也可以通过 xaml 完成)

If you are using Silverlight 3.0, then using CollectionViewSource is the cleanest way. Refer below example: (it can be done via xaml as well)

ObservableCollection<DateTime> ecAll = new ObservableCollection<DateTime>();
CollectionViewSource sortedcvs = new CollectionViewSource();
sortedcvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Date", 
    System.ComponentModel.ListSortDirection.Ascending));
sortedcvs.Source = ecAll;
ListBoxContainer.DataContext = sortedcvs;

并在相应的xaml集合中

And in corresponding xaml set

ItemsSource="{Binding}"

用于 ListBox 或任何 ItemsControl 派生控件

for the ListBox or any ItemsControl derived control

这篇关于使用 linq 对可观察集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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