如何对 EntitySet进行排序

发布于 2024-07-21 07:27:03 字数 454 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丶情人眼里出诗心の 2024-07-28 07:27:03

实体集没有实现 IBindingList...它提供了一种获取 IBindingList 的方法。 您需要调用 .GetNewBindingList() 来获取 EntitySetBindingList的实例,该实例派生自 SortableBindingList,它是一个 BindingList。 EntitySetBindingList 只是原始 EntitySet的包装器。 它是由它创建的,因此对它的任何修改都是对原始 EntitySet 的修改。

编辑:使用 BindingList 排序:

要使用 BindingList 排序,您需要公开某种接口以允许排序。 BindingList内支持排序。 类,但它是通过受保护的属性和方法实现的。 应该可以使用包装器公开表达性排序方法:

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

然后您应该能够像这样排序:

var bindingWrapper = new EntitySetBindingWrapper(myEntitySet.GetNewBindingList());
bindingWrapper.Sort(e => e.MyProperty, ListSortDirection.Ascending);

listView.DataSource = bindingWrapper;

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:

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.

失而复得 2024-07-28 07:27:03

按降序排列!

var tags = objContext.Tags;
gvTester.DataSource = tags.OrderByDescending(x => x.TagID); ;
gvTester.DataBind();

OrderByDecending!

var tags = objContext.Tags;
gvTester.DataSource = tags.OrderByDescending(x => x.TagID); ;
gvTester.DataBind();
层林尽染 2024-07-28 07:27:03

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

Could you do .ToList().OrderBy(x=>x.FieldToSortBy) ?

软糖 2024-07-28 07:27:03

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 的人员列表

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 可以以类似的方式进行排序,无论是原始类型还是复杂对象。

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:

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

now, assume I've got a list of Person set up called People

var people = new List<Person>();

And I've got a whole bunch of people in my list.

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

That's for complex objects. If we've got a list of some primitive type, like a string:

var strings = new List<string>();

I want to sort them based on themselves - i.e. not by some property of my object

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文