问题描述
我在 BindingList
中存储了数千个 MyClass 对象.我想按日期属性 MyClass.dt
对它们进行排序.
I have several thousands MyClass objects stored in BindingList<MyClass>
. I want to sort them by date property MyClass.dt
.
Class BindingList 不支持直接排序.如何对 BindingList
进行排序而不复制所有对象?我需要按升序和降序对它们进行排序.
Class BindingList doesn't support sorting directly. How I can sort BindingList<T>
not making duplicate copies of all objects? I need to sort them as in ascending, as in descending order, please.
我不需要 .我正在寻找一两行代码的简短解决方案.
I don't need special class SortableBindingList
as described in BindingList.Sort() to behave like a List.Sort(). I am searching for short solution in one o two lines of code.
推荐答案
Linq 可以工作.
var sortedListInstance = new BindingList<MyClass>(unsortedListInstance.OrderBy(x => x.dt).ToList());
请记住,您获得的是排序列表的浅表副本,而不是 MyClass
的重复实例.
Keep in mind you get a shallow copy of the sorted list, not duplicate instances of MyClass
.
不要忘记在代码文件顶部包含命名空间System.Linq
这篇关于如何对 BindingList<T> 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!