如何对 EntitySet进行排序
MSDN 文档指出 EntitySet 实现了 IBindingList
(请参阅 http 处的“绑定到 EntitySets”) ://msdn.microsoft.com/en-us/library/bb546190.aspx)
但是,可以清楚地看到EntitySet并没有实现这个接口!
那么我该如何排序呢?
对于上下文,我将此集绑定到 WPF ListView。
有关我试图解决的问题的更广泛背景,请参阅这篇文章。
The MSDN documentation states that an EntitySet implements IBindingList
(see 'Binding to EntitySets' at http://msdn.microsoft.com/en-us/library/bb546190.aspx)
However, it can be clearly seen that an EntitySet does not implement this interface!
So how do I sort?
For context, I'm binding this set to a WPF ListView.
For wider context of problem I'm trying to solve please see this post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实体集没有实现 IBindingList...它提供了一种获取 IBindingList 的方法。 您需要调用 .GetNewBindingList() 来获取 EntitySetBindingList的实例,该实例派生自 SortableBindingList,它是一个 BindingList。 EntitySetBindingList 只是原始 EntitySet的包装器。 它是由它创建的,因此对它的任何修改都是对原始 EntitySet 的修改。
编辑:使用 BindingList 排序:
要使用 BindingList 排序,您需要公开某种接口以允许排序。 BindingList内支持排序。 类,但它是通过受保护的属性和方法实现的。 应该可以使用包装器公开表达性排序方法:
然后您应该能够像这样排序:
EntitySetBindingWrapper 类可能有其他实现...例如转发 BindingList上的任何通常公共方法 到提供给构造函数的那个。
EntitySet<T> doesn't implement IBindingList...it provides a method to get an IBindingList. You need to call .GetNewBindingList() to get an instance of EntitySetBindingList<T>, which derives from SortableBindingList<T>, which is a BindingList<T>. EntitySetBindingList is just a wrapper around the original EntitySet<T> it was created from, so any modifications to it are modifications to the original EntitySet.
EDIT: Sorting with BindingList:
To sort with a BindingList, you need to expose some kind of interface to allow sorting. Sorting is supported within the BindingList<T> class, but its through protected properties and methods. It should be possible to expose an expressive sort method with a wrapper:
You should then be able to sort like so:
There might be additional implementation for the EntitySetBindingWrapper class...such as fortwarding any normally public methods on BindingList<T> to the one provided to the constructor.
按降序排列!
OrderByDecending!
你能做 .ToList().OrderBy(x=>x.FieldToSortBy) 吗?
Could you do .ToList().OrderBy(x=>x.FieldToSortBy) ?
Hainesy - 从你对 Vinny 的帖子的评论来看,我认为你没有抓住他的观点...... [我不会直接将此作为你问题的答案,我只是详细阐述 Vinny 的观点以消除任何可能的混乱关于这一点。]
考虑这个对象:
现在,假设我有一个名为 People 的人员列表
,并且我的列表中有一大堆人。
那是针对复杂的对象。 如果我们有一些原始类型的列表,例如字符串:
我想根据它们本身对它们进行排序 - 即不是根据我的对象的某些属性
这将根据对象进行排序。 如果您要对实现 IComparable 的复杂对象进行排序以按其默认比较器进行排序,您也可以使用相同的想法。
EntitySet 可以以类似的方式进行排序,无论是原始类型还是复杂对象。
Hainesy - judging by your comment on Vinny's post, I think you're missing his point... [I'm not presenting this as an answer to your question directly, I'm just elaborating on Vinny's point to clear up any possible confusion regarding that.]
Consider this object:
now, assume I've got a list of Person set up called People
And I've got a whole bunch of people in my list.
That's for complex objects. If we've got a list of some primitive type, like a string:
I want to sort them based on themselves - i.e. not by some property of my object
This will sort on the object. You can also use the same idea if you're sorting complex objects that implement IComparable to sort by their default comparer.
An EntitySet can be sorted in a similar fashion, whether for a primitive type, or a complex object.