如何排序EntitySet< T> [英] How do you sort an EntitySet<T>
问题描述
MSDN文档指出,EntitySet实现IBindingList
The MSDN documentation states that an EntitySet implements IBindingList
(请参阅 http://msdn.microsoft.com/en-us/library/bb546190.aspx )
但是,可以清楚地看到,EntitySet不实现这个界面!
However, it can be clearly seen that an EntitySet does not implement this interface!
那么我如何排序?
对于上下文,我将此集合绑定到WPF ListView。
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.
推荐答案
EntitySet< T>不实现IBindingList ...它提供了一种获取IBindingList的方法。您需要调用.GetNewBindingList()来获取EntitySetBindingList< T>的实例,该实例来自SortableBindingList< T>,其是BindingList< T>。 EntitySetBindingList只是原始EntitySet< T>它是由...创建的,所以对其进行的任何修改都是对原始EntitySet的修改。
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.
编辑:使用BindingList排序:
Sorting with BindingList:
要使用BindingList进行排序,您需要公开某种界面以允许排序。 BindingList< T>中支持排序类,但它通过受保护的属性和方法。应该可以使用包装器公开一个表达式的排序方法:
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:
public class EntitySetBindingWrapper<T>: BindingList<T>
{
public EntitySetBindingWrapper(BindingList<T> root) : base(root)
{
}
public void Sort(Expression<Func<T, P>> expr, ListSortDirection direction)
{
if (expr == null)
base.RemoveSortCore();
MemberExpression propExpr = expr as MemberExpression;
if (propExpr == null) throw new ArgumentException("You must provide a property", "expr");
PropertyDescriptorCollection descriptorCol = TypeDescriptor.GetProperties(typeof(T));
IEnumerable<PropertyDescriptor> descriptors = descriptorCol.Cast<PropertyDescriptor>();
PropertyDescriptor descriptor = descriptors.First(pd => pd.Name == propExpr.Member.Name);
base.ApplySortCore(descriptor, direction);
}
}
您应该可以像这样排序: / p>
You should then be able to sort like so:
var bindingWrapper = new EntitySetBindingWrapper(myEntitySet.GetNewBindingList());
bindingWrapper.Sort(e => e.MyProperty, ListSortDirection.Ascending);
listView.DataSource = bindingWrapper;
对于EntitySetBindingWrapper类,可能还有其他实现,例如在BindingList< ; T>给提供给构造函数的一个。
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.
这篇关于如何排序EntitySet< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!