sorting and filtering in ReadOnlyListBase

sorting and filtering in ReadOnlyListBase

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


pillesoft posted on Tuesday, October 20, 2009

i'm new to csla, especially working with linq, and databinding. from projecttrackercs, and other examples i was able to create my own objects. i have a ROLB with ROB objects.
i want to display the data on a win gui in a grid.
so far so good...
but i would like to filter the data, and sort the data.
i thought that the filter i can manage with some linq query
and the sorting with the SortedBindingList object.

_list is the ROLB.

but i'm not able to implement both requirements.
just filtering is working:

        var list = from r in _list where r.Name.Contains(textBox1.Text) select r;
        this.mainItemListBindingSource.DataSource = list;

just sorting is also working:
        this.mainItemListBindingSource.DataSource = new SortedBindingList<MainItemInfo>(_list);

how can i filter and sort the data?

another question relating to linq query.
in the first example (just filtering), is there any database read action? or is it just filtering the collection in memory?

thanks in advance
Ivan
 

RockfordLhotka replied on Wednesday, October 21, 2009

Windows Forms data binding and LINQ aren't fully compatible.

LINQ to Objects returns IEnumerable<T> collections - the simplest type of collection in .NET.

Windows Forms data binding is designed to work with BindingList<T> collections, which have more features.

So binding to a L2O query result in Windows Forms only works in the most simple scenarios.

Of course a read-only list is a simple scenario, and so what you are trying to do should generally work.

However, you might have better luck using the SortedBindingList and FilteredBindingList classes provided by CSLA .NET. These pre-date L2O and are designed primarily to support sorting and filtering in a way that works well with Windows Forms data binding.

Again, for your simpler scenario, L2O queries should work fine. Because this is LINQ to Objects, there is no database access - LINQ is just looping through the items in your collection and creating a new collection that only contains the items you specify based on your where clause.

pillesoft replied on Wednesday, October 21, 2009

ok, thanks

i know there is a LinqBindingList object in 3.8, but i couldn't find any tutorial for that. is it more compatible with windows forms?

actually what i have missed was very simple.
var list = _list.SearchByExpression(x => x.Name.Contains(textBox1.Text));
this.mainItemListBindingSource.DataSource = new SortedBindingList<MainItemInfo>(list.ToList());

i'll try to use ROLB objects in the grid as much as possible, and display a dialog if the user adds or modifies a record with BO.

i have some data which are not so volatile, there i can use a fetch at the beginning and after just work in memory.
how filteredbindinglist filters the data? is it accessing the database or working in memory?

Ivan

RockfordLhotka replied on Wednesday, October 21, 2009

Yes, the purpose of LinqBindingList is to provide better Windows Forms support when doing a query over a BusinessListBase.

 

There’s no real tutorial because it is automatic. Any identity projection query over a BusinessListBase will return a LinqBindingList – that just happens with no effort. And LinqBindingList is a BindingList<T>, and is also a view over the original list, so any insert/update/remove operations affect the real list as well as the LBL view. Again, all automatic.

 

FilteredBindingList is also a view over the original list, and so doesn’t talk to the database. In fact LinqBindingList is just a variation on the pattern established by SortedBindingList and FilteredBindingList – all three are just bindable views over the real collection.

 

JoeFallon1 replied on Wednesday, October 21, 2009

To be clear, the LinqBindingList is only created for Editable Collections.
It is NOT created for Read Only Collections.

Also, there is a class called ObjectListView in CSLA Contrib which I used to use prior to LINQ. It has the advantage of being able to sort more than one field at a time which is a limitation of SortedBindingList.

Joe

 

 

mikemir8 replied on Wednesday, January 20, 2010

I know this thread is a little bit old, but I'm just starting to play around with csla 3.8 (was using 3.0), and I'm still trying to wrap my head around a few concepts, and linqbindinglist is one of them.

The book mentions that sortedbindinglist and filteredbindinglist are still there just for backwards compatibility, and that linqbindinglist should replace them. However, by looking at Rocky's posts in this thread, it appears that they're still a valid option specially in windows applications. I have gotten it to work (to some extent) with linqbindinglist, but I have found some issues that make me wonder if sortedbindinglist would be a better option. But I don't want to start something new on classes that will be no longer supported.

So, what is the current status of sortedbindinglist? What is its future?

JonnyBee replied on Wednesday, January 20, 2010

Hi,

You should view Sorting and Filtering at a UI level - and select the technology that suits your UI technology best. SortedBindingList and FilteredBindingList works very well with Windows Forms whereas LinqBindingList fits good with WPF and Silverlight.

Copyright (c) Marimer LLC