Search code examples
c#linqbindinglist

Why does the type of my DataSource seem to change when I call .OrderBy on it before passing it to a BindingSource?


I'm having a problem I hope someone can help me with.

I have a class, EmployeeList that inherits from:

public class MultipleSortableBindingListView<T> : BindingList<T>, IBindingListView

I fill the EmployeeList, and set a BindingSource.DataSource equal to it:

EmployeeListBindingSource.DataSource = lstEmployees.OrderBy(t=>t.LastName)
   .ThenBy(t=>t.FirstName);

Then, when I reference the DataSource, it is no longer of type EmployeeList.

Basically, I need to cast back to EmployeeList after the OrderBy is performed.


Solution

  • You will probably have to do something like:

    var newlist = new EmployeeList(lstEmployees.OrderBy(t=>t.LastName).ThenBy(t=>t.FirstName));
    

    However, since EmployeeList derives from a class with the name MultipleSortableBindingListView one would suspect that it has it's own means of setting the sort order, without have to resort to using LINQ.