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

查看:68
本文介绍了使用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天全站免登陆