BindingSource返回按字母顺序排序的列表的属性

时间:2018-01-16 16:12:49

标签: c# linq bindingsource

我正在尝试创建一个按字母顺序返回BindingSource的属性,此处是“Name”列。下面是使用Linq的镜头,它没有编译:'BindingSource不包含OrderBy的定义'

我不知道如何实现这一点,Linq或其他 你有什么建议?

internal System.Windows.Forms.BindingSource bsContractors;
this.bsContractors = new 
System.Windows.Forms.BindingSource(this.components);

public System.Collections.Generic.List<Contractor> Contractors
{
    get
    { 
        // Linq
        List < Contractor > SortedList = bsContractors.OrderBy(o => o.Name).ToList();

        bsContractors.DataSource = SortedList;

        return bsContractors.List as System.Collections.Generic.List<Contractor>;

    }
}

1 个答案:

答案 0 :(得分:1)

我只是在这里猜测,但试试这个:

 List <Contractor> SortedList = bsContractors.List.Cast<Contractor>().OrderBy(o => o.Name).ToList();