Search code examples
c#linqbindingsource

Property for BindingSource to return alphabetically sorted list


I'm trying to create a property that returns a BindingSource in alphabetical order, here by the column "Name". Below is a shot at using Linq, which doesn't compile: 'BindingSource does not contain a defintion for OrderBy'

I have no preference in how this is accomplished, Linq or otherwise.
What do you suggest?

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

    }
}

Solution

  • I'm just guessing here, but try this:

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