Linq Sort in ApplySortCore

Linq Sort in ApplySortCore

Old forum URL: forums.lhotka.net/forums/t/6960.aspx


Topher posted on Monday, May 18, 2009

Is the best place to implement a Linq sort in the interface? Is it possible to implement a Linq sort in the ApplySortCore override of a BusinessListBase class?

I'm interested in finding a way to implement a reusable custom sort for a BusinessListBase collection when a user clicks on a column header and implementing the sort in the interface leads to some unnecessary duplicate code. I know I can implement the sort by implementing IComparer, but I'd rather use Linq if I can.

RockfordLhotka replied on Monday, May 18, 2009

No, that won't work.

LINQ to Objects results in a new collection being created. It doesn't sort or filter a collection in-place.

ApplySortCore() and the related IBindingList members work on a collection in-place. They represent an older thought model Microsoft was pursuing prior to LINQ, and the two ideas are really incompatible.

It also turns out that the older model is harder than it might at first appear. This is why CSLA includes the SortedBindingList and FilteredBindingList classes - to act as sortable/filterable wrappers around existing collections. If you are using Windows Forms, this is probably your best bet, not LINQ.

Topher replied on Monday, May 18, 2009

Thanks for the quick reply Rocky! That helps clear up some confusion I had regarding sorting implementation.

I'm casting my collection to a SortableBindingList and the sort is working great, but I'm getting an "EditLevel" error when I try to edit the collection.

Does this mean my child collection needs a datasource which is pointed to the root type of my collection and is handled separate from the parent? Right now the bindingsource is pointed to a property in the parent bindingsource which exposes the child collection and everything is saved all at once.

RockfordLhotka replied on Monday, May 18, 2009

Windows Forms data binding is a world unto itself...

Yes, you need a bindingsource per object. If you use drag-and-drop in the
designer from the data sources window you'll usually get the right
combination - just change the way the child collection's bindingsource is
set up so it uses your sortedbindinglist instead of the actual list.

The CslaActionExtender control can also help a lot, otherwise you have to
write all that ugly code behind every form to deal with binding and
unbinding at every level.

Rocky

Topher replied on Monday, May 18, 2009

Thanks again for your help Rocky!! I'd just like your advice on my choice of implementation and I think I've got it figured out.

I implemented the SortableBindingList as a property in my parent object and everything works as expected. Is this the best way to implement a SortableBindingList or am I completely missing the boat? Will I have problems with serialization if I use this solution?

Most of the examples on the forum implement a SortableBindingList in the UI, but I wasn't able to get things to save correctly when I did this. I've been using the CslaActionExtender control and it's working great for me. It definitely obfuscates a lot of the WinForm databinding hassle!

RockfordLhotka replied on Monday, May 18, 2009

I think you may have serialization issues - SortedBindingList really is
intended as a UI helper, and isn't designed for the business layer.

It can be tricky to get the bindingsource to use the SortedBindingList
though, but it has been a while since I spent much time in that area, so
hopefully someone with more recent experience can jump in with suggestions.

Rocky

Topher replied on Tuesday, May 19, 2009

No problem, I'll keep working at it and try and keep it in the UI. Thanks for your help Rocky!

Copyright (c) Marimer LLC