1

MSDN 文档指出 EntitySet 实现了 IBindingList

(请参阅http://msdn.microsoft.com/en-us/library/bb546190.aspx上的“绑定到实体集” )

但是,可以清楚的看到一个EntitySet并没有实现这个接口!

那么我该如何排序呢?

对于上下文,我将此集绑定到 WPF ListView。

有关我要解决的问题的更广泛背景,请参阅这篇文章

4

4 回答 4

7

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:

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);
    }
}

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;

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.

于 2009-05-29T14:49:05.113 回答
1

OrderByDecending!

var tags = objContext.Tags;
gvTester.DataSource = tags.OrderByDescending(x => x.TagID); ;
gvTester.DataBind();
于 2011-11-10T22:38:38.120 回答
0

你能做 .ToList().OrderBy(x=>x.FieldToSortBy) 吗?

于 2009-05-29T13:51:57.240 回答
0

Hainesy - 从你对 Vinny 帖子的评论来看,我认为你错过了他的观点...... [我没有直接提出这个作为你问题的答案,我只是详细阐述 Vinny 的观点以消除任何可能的混淆关于那个。]

考虑这个对象:

public class Person
{
    public string FirstName;
    public string MiddleInitial;
    public string LastName;

    public DateTime DateOfBirth { get; set; }

    public int Age
    {
        get
        {
            return (int)DateTime.Today.Subtract(DateOfBirth).TotalDays / 365;
        }
    }
}

现在,假设我有一个名为 People 的 Person 列表

var people = new List<Person>();

我的名单上有一大堆人。

var sortedByLastName = people.OrderBy(o => o.LastName);
var sortedByFirstName = people.OrderBy(o => o.FirstName);
var sortedByAge = people.OrderBy(o => o.Age);
var sortedByAgeDesc = people.OrderByDescending(o => o.Age);
var sortedByLastThenFirst = people.OrderBy(o => o.LastName).ThenBy(o => o.FirstName);

这适用于复杂的对象。如果我们有一些原始类型的列表,比如字符串:

var strings = new List<string>();

我想根据自己对它们进行排序 - 即不是按我的对象的某些属性

var sorted = strings.OrderBy(s => s);

这将对对象进行排序。如果您正在对实现 IComparable 的复杂对象进行排序以按其默认比较器排序,您也可以使用相同的想法。

EntitySet 可以以类似的方式排序,无论是对于基本类型还是复杂对象。

于 2009-05-29T14:48:26.550 回答