Sorting / Filtering EditableRootList

Sorting / Filtering EditableRootList

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


Crestline posted on Monday, June 26, 2006

Is it possible?

We looked at the SortedBindingList but it looks like it will only work with ReadOnlyLists.  Just wondering if we are missing something or not.

Thanks.

Brian Criswell replied on Monday, June 26, 2006

It works with any List<T>.  Both BusinessBase and ReadOnlyBase support this.  I must be missing your meaning.

RockfordLhotka replied on Monday, June 26, 2006

Yes, SortedBindingList is designed to act as a live, editable "view" over any IList<T>.

The new FilteredBindingList coming in 2.1 will do the same thing, but acting as a filter instead.

And since they both implement IList<T> themselves, they'll be stackable - which I find very cool:

List<string> list = new List<string>();
FilteredBindingList<string> f = new FilteredBindingList<string>(list);
f.ApplyFilter("Rocky");
SortedBindingList<string> s = new SortedBindingList<string>(f);
s.ApplySort();

The result displays only items containing "Rocky" and in sorted order Smile [:)]

brembot replied on Monday, December 11, 2006

How am i going to filter multiple values in a property?

Ex. StudentNo Like "1,5,7"

RockfordLhotka replied on Monday, December 11, 2006

If you are using FilteredBindngList you can create your own filter provider method to do virtually anything you'd like.
 
Public Module StudentFilter
 
  Public Function Filter(ByVal item As Object, ByVal filter As Object) As Boolean
 
    Dim value As String = item.ToString
    ' assume filter is an array of string
    Dim list() As String = CType(filter, String())
    For Each item As String In list
      If value.Contains(item) Then
        ' found a match
        Return True
      End If
    Next
    ' didn't find a match
    Return False
 
  End Function
 
End Class
 
To use this, set the FilterProvider property of the FilteredBindingList to the address of (delegate pointer to) this Filter method.
 
Dim list As StudentList = StudentList.GetList
Dim filteredList As New FilteredBindingList(list, AddressOf StudentFilter.Filter)
Dim limit() As String = {"1", "5", "7"}
filteredList.ApplyFilter("StudentNo", limit)
 
 
Rocky
 


From: brembot [mailto:cslanet@lhotka.net]
Sent: Monday, December 11, 2006 8:27 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Sorting / Filtering EditableRootList

How am i going to filter multiple values in a property?

Ex. StudentNo Like "1,5,7"



brembot replied on Tuesday, December 19, 2006

Hello Rocky,

Thanks for your reply and it works. One more thing, How about filtering two or more property in a list? How am i going to achieve that?

Thanks in advance.
Marlon

RockfordLhotka replied on Tuesday, December 19, 2006

If you pass Nothing/null for the property name, then the business object itself is passed into your filter method as the item parameter.

If the caller needs to dynamically specify the property names to use, you can just make your filter parameter (the second parameter) a more complex object that contains both the list of property names and the list of other filter criteria.

Inquistive_Mind replied on Tuesday, January 23, 2007

RockfordLhotka:

Yes, SortedBindingList is designed to act as a live, editable "view" over any IList<T>.

The new FilteredBindingList coming in 2.1 will do the same thing, but acting as a filter instead.

And since they both implement IList<T> themselves, they'll be stackable - which I find very cool:

List<string> list = new List<string>();
FilteredBindingList<string> f = new FilteredBindingList<string>(list);
f.ApplyFilter("Rocky");
SortedBindingList<string> s = new SortedBindingList<string>(f);
s.ApplySort();

The result displays only items containing "Rocky" and in sorted order Smile [:)]

 

Hello Rocky,

I am trying to filter out an Editable List Object and bind it to the the grid but the ApplyFilter() method signature requires two Paramterers.Is there something I am missing?I want to filter a list by just a string and I do not want to create another object and pass the criteria object to it.Any thoughts on this?

 

Thanks in Advance

V

RockfordLhotka replied on Tuesday, January 23, 2007

Inquistive_Mind:

I am trying to filter out an Editable List Object and bind it to the the grid but the ApplyFilter() method signature requires two Paramterers.Is there something I am missing?I want to filter a list by just a string and I do not want to create another object and pass the criteria object to it.Any thoughts on this?

This is suppored - just pass string.Empty or "" as the property name. That'll result in the filter running against the ToString() value of the entire object. So as long as your object puts the appropriate values into its ToString() output you are all set.

The only alternative is to write your own custom filter method, so it could be aware of all the different properties of your specific object and how you want to deal with them.

seamusc replied on Tuesday, January 08, 2008

Hi Rocky,

I am using the CSLA version 2.0.0.0 I am afraid for now but I would be keen to know how you would suggest I can Filter a DataGridView that is bound to BusinessListBase object. For sorting I can simply use

SortedBindingList<TaskType> sortedList = new SortedBindingList<TaskType>(taskTypeList);
sortedList.ApplySort("DisplayOrder", ListSortDirection.Ascending);
this.bindingSource1.DataSource = sortedList;

but i am not sure how I can achive the same for filtering in this version of the CSLA

regards

Shay

ajj3085 replied on Tuesday, January 08, 2008

Csla 2.x versions are depreciated.  Can you upgrade to Csla 3?  You don't need .net framework 3 to use it.

RockfordLhotka replied on Tuesday, January 08, 2008

I suppose you could try back-porting FilteredBindingList into CSLA 2.0.

 

Or you can look at CSLAcontrib on CodePlex, as there’s a contribution there that may help (ObjectListView or something like that?)

 

Rocky

JohnWester replied on Thursday, September 11, 2008

it appears using the SortedBindingList you can only set one property name in the applySort, so you can only sort one column on a grid at a time.  Is this correct?  Is there a way to sort on multiple columns?

JoeFallon1 replied on Thursday, September 11, 2008

Yes it is correct.

No you cannot sort on multiple columns with it.

I use the ObjectListView instead - it does sorting and filtering on multiple columns.

It is available for free in the CSLA Contrib download section.

Check old posts here for samples on how to use it.

Oh - and once you move up to .Net 3.5 you can use LINQ to do sorting and filtering. I haven't got there yet. Maybe around Xmas I can move in to the modern world  - nice way to end the year!

Joe

RockfordLhotka replied on Thursday, September 11, 2008

It is true that the implementation is relatively simplistic.

 

On codeplex there’s a more complex implementation.

 

But I think the real answer is LINQ to CSLA, and so I view SortedBindingList and FilteredBindingList as effectively obsolete at this point.

 

Rocky

PC Tech replied on Wednesday, November 12, 2008

I'm using Csla 3.5.2 and I am trying to wean myself off of the SortedBindingList and FilteredBindingList and to use Linq instead. I'm hitting a bit of a snag though. When using a SortedBindingList, any change I make to the original BLB collection is immediately reflected in the SBL including adding and removing items. On the other hand, when I use Linq if I add or remove from the underlying collection the change does not appear automatically. I need to maunally re-run the Linq query to see the changes. Is there a way to have the collection returned by the Linq query to work dynamically?

Old Way

SortedBindingList<ShippingWeightTableItem> sbl = new SortedBindingList<ShippingWeightTableItem>(WeightTable);
sbl.ApplySort(
"Weight", ListSortDirection.Ascending);
bindingSource2.DataSource = sbl;

New Way

bindingSource2.DataSource = from w in WeightTable orderby w.Weight ascending select w;

JoeFallon1 replied on Wednesday, November 12, 2008

If you use "regular" LINQ to Objects then the result is IEnumerable(Of T) and it is not "linked" (pun intended) to the source collection.

If you use LINQ to CSLA then the result is a collection of the same type as the source and the two are linked so that changes in the result are reflected in the source.

I am not sure which version of CSLA this appears in first. 3.5 or 3.6.

Joe

 

maxal replied on Tuesday, December 23, 2008

What is the LINQ to CSLA? Are there examples how to use in instead of "regular" LINQ to Objects?

RockfordLhotka replied on Tuesday, December 23, 2008

The Expert C# 2008 Business Objects book has an entire chapter (14) devoted to LINQ to CSLA.

 

LINQ to CSLA has two features:

 

1.       When doing a non-projection query against a BLB you get back a LinqBindingList<T> instead of an IEnumerable<T>. The LinqBindingList class is much like SortedBindingList or FilteredBindingList, in that it is a live view over the real BLB you started with. This means if you add/remove items from the result of the query, they are added/removed in the real BLB too.

2.       Indexed queries against a BLB. If you mark a child object’s property with the Indexable attribute, and then use that property in the where clause of a LINQ query against the list, CSLA will create an index against that property and will use the index for the query. This is great if you do numerous queries against a single instance of a large list – in which case you can get many orders of magnitude better performance. If you don’t do multiple queries against a single large list, it is actually counter-productive, because creating the index is more expensive than the benefit of using it.

 

No examples of using it are really needed, because it just works. There’s no special syntax or special effort on your part. If you do a non-projection query against a BLB, you get a LBL<T> back. If you put the Indexable attribute on a child property, and use that property (alone) in a where clause, you’ll get indexing.

 

Rocky

 

maxal replied on Tuesday, December 23, 2008

Thank you, I have to read the book, I know that. Couple of questions before that, if I may.

Am I right that LinqBindingList<T> is not used when I use Linq against RLB (read-only) list?
Also, I see that indexes are used for filtering (where clause in Linq) but I could not find them being used for sorting. Are they used for that?

RangerGuy replied on Tuesday, December 19, 2006

I'm trying to figure this out as well....

I have to filter on a property that is of type enum. The filter doesn't seem to work on anything but a string and when I create a custom filter function.. it sets both params as the filter value.

If I find an answer I will post it for ya :).

Mike.Smith replied on Tuesday, January 23, 2007

Has a solution the the structure problem been discovered yet? I am trying to filter based on a Guid (as I suspect others may want to do) but it doesn't seem to work out-of-the-box.

The error I get is: "System.InvalidCastException: Conversion from type 'Guid' to type 'String' is not valid."

What is the preferred work-around?

Thanks in advance,

Mike

Mike.Smith replied on Tuesday, January 23, 2007

In case anyone else is having a bit of trouble with Enums/Structures/Guids and filtering, I may have a solution:

    Public Shared Function Filter(ByVal item As Object, _
                                            ByVal filterValue As Object) As Boolean

        Dim result As Boolean = False

        If item IsNot Nothing AndAlso _
           filterValue IsNot Nothing Then

            result = item.ToString.Equals(filterValue.ToString)
        End If

        Return result

    End Function

It works like a charm for me and I can't find a problem with it but of course that doesn't mean there isn't a problem. Smile [:)] Feedback would be appreciated.

Mike

Copyright (c) Marimer LLC