.net 如何对实体集进行排序<;T>;

.net 如何对实体集进行排序<;T>;,.net,linq-to-sql,data-binding,sorting,entityset,.net,Linq To Sql,Data Binding,Sorting,Entityset,MSDN文档说明EntitySet实现IBindingList (请参阅上的“绑定到EntitySets”) 然而,可以清楚地看到EntitySet并没有实现这个接口 那我该怎么分类呢 对于上下文,我将此集合绑定到WPF ListView 关于我试图解决的问题的更广泛的背景,请参见。你能做点什么吗.ToList().OrderBy(x=>x.fieldtosorby)?你能做点什么吗.ToList().OrderBy(x=>x.fieldtosorby)?海内西-从你对文尼帖子的评论来看,我认为

MSDN文档说明EntitySet实现IBindingList

(请参阅上的“绑定到EntitySets”)

然而,可以清楚地看到EntitySet并没有实现这个接口

那我该怎么分类呢

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


关于我试图解决的问题的更广泛的背景,请参见。

你能做点什么吗.ToList().OrderBy(x=>x.fieldtosorby)?

你能做点什么吗.ToList().OrderBy(x=>x.fieldtosorby)?

海内西-从你对文尼帖子的评论来看,我认为你没有抓住他的重点。。。[我并不是直接回答你的问题,我只是详细阐述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;
        }
    }
}
现在,假设我有一张叫做“人”的名单

var people = new List<Person>();
这是针对复杂对象的。如果我们有一些基本类型的列表,比如字符串:

var strings = new List<string>();
这将对对象进行排序。如果要对实现IComparable的复杂对象进行排序,也可以使用相同的方法,以按照默认比较器进行排序


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

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;
        }
    }
}
现在,假设我有一张叫做“人”的名单

var people = new List<Person>();
这是针对复杂对象的。如果我们有一些基本类型的列表,比如字符串:

var strings = new List<string>();
这将对对象进行排序。如果要对实现IComparable的复杂对象进行排序,也可以使用相同的方法,以按照默认比较器进行排序

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

EntitySet没有实现IBindingList…它提供了获取IBindingList的方法。您需要调用.GetNewBindingList()来获取EntitySetBindingList的实例,该实例派生自SortableBindingList,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);
    }
}
EntitySetBindingWrapper类可能还有其他实现…例如将BindingList上的任何正常公共方法转换为提供给构造函数的方法。

EntitySet没有实现IBindingList…它提供了一个获取IBindingList的方法。您需要调用.GetNewBindingList()来获取EntitySetBindingList的实例,该实例派生自SortableBindingList,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);
    }
}
EntitySetBindingWrapper类可能还有其他实现…例如将BindingList上的任何正常公共方法转换为提供给构造函数的方法。

OrderByDecenting

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

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

嗨,谢谢。。。但我的问题是如何找出如何直接对实体集进行排序。我知道如何对列表进行排序。当我说“这意味着创建另一个属性”时,我并不是指要排序的属性。如果你阅读我在问题底部链接到的帖子,问题会更复杂一些。嗨,谢谢。。。但我的问题是如何找出如何直接对实体集进行排序。我知道如何对列表进行排序。当我说“这意味着创建另一个属性”时,我并不是指要排序的属性。如果你阅读我在问题底部链接到的帖子,问题会更复杂一些。谢谢,我不确定这会不会让我走上一条黑暗的小巷。如果你阅读了我在问题底部链接到的帖子,可能在数据绑定方面我缺少了一些东西。因此,在阅读了另一篇帖子后,他错误地调用了.ToList()。绑定源可能正在更新其基础列表,但该列表不是EntitySet。调用.ToList创建一个新的列表对象。BindingList支持排序…有点像一种古老的形式…但它必须公开。您需要创建一个BindingList包装器,该包装器可以包装GetNewBindingList()返回的内容,并公开SortPropertyCore和SortDirectionCore。我将用一个示例发布另一个答案。EntitySet通过委托给您提到的GetNewBindingList()实现IListSource.GetList()(并缓存IBindingList结果)。如果通过BindingSource将EntitySet绑定到WinForms DataGridView,则会自动获得列排序支持。我不知道WPF,但我认为它也会起到类似的作用。我想这取决于您是希望用户能够排序,还是只希望向用户提供无法进一步排序的已排序列表。如果是后者,那么这将允许您在绑定之前进行排序。谢谢,我不确定这是否会让我走上一条黑暗的小巷。如果你阅读了我在问题底部链接到的帖子,可能在数据绑定方面我缺少了一些东西。因此,在阅读了另一篇帖子后,他错误地调用了.ToList()。绑定源w