Trouble with ApplyFilter in FilteredBindingList

Trouble with ApplyFilter in FilteredBindingList

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


c_shah posted on Saturday, October 27, 2007

I have tried to implement filtered list as you have suggested in this post http://forums.lhotka.net/forums/thread/18628.aspx and described in the handbook for CSLA 2.1, filter does String contains (or sQL Like) when you use ApplyFilter ("property name", object as expression)

How can I achieve Equals instead of contains


For e.g.
I have read only collection, each object in the collection has following properties

SubCarrier_ID (int)
SubCarrier_Name (String)
Carrier_ID (int)

When I do applyfilter

Dim filtered as CSLA.FilteredBindingList(of SubCarrier)(SubCarrierList)
Filtered.ApplyFilter("CARRIER_ID", 1) ' Where 1 is the id of the Carrier

filtered now contains all the subcarriers with carrier__id including Carrier_ID 1, 10 ,100

I only want to filter objects with Carrier_ID = 1 (as you do in SQL WHERE Carrier_ID = 1)

Read about custom filter provider but looks like i am making it complicated (I am a beginner of CSLA and in .NET you can use Filter with DataView where it does WHERE)

 

RockfordLhotka replied on Sunday, October 28, 2007

No, you do need to create a custom filter for anything that the default filter doesn't do. The default filter calls Contains() (you can look at the code - it isn't complex).

You can do equals by creating a different filter like:

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

 
Dim result As Boolean = False
 
If Not item Is Nothing AndAlso Not filterValue Is Nothing Then
   
result = CStr(item).Equals(CStr(filterValue))
 
End If
 
Return result

End Function

JoeFallon1 replied on Monday, October 29, 2007

Here is some sample code that may help when using FBL:

Protected Function GetDataSource(ByVal key As Integer) As FilteredBindingList(Of MyBo)
 
Dim filteredList As FilteredBindingList(Of MyBo)
  
Dim filterValue As Object

 
filteredList = New FilteredBindingList(Of MyBo)(mMyBOECC)

 
If filteredList IsNot Nothing Then
   
filteredList.FilterProvider = AddressOf MyBoFilter
    filterValue = key
    filteredList.ApplyFilter(
"", filterValue)  'Passing "" means you pass the whole BO not a Property.
 
End If

  Return filteredList
End Function

'The primary requirement is that the method signature conform to the FilterProvider delegate.
'The method must return True for items that should be included in the filtered view, and False for those that should not.
Public Shared Function MyBoFilter(ByVal item As Object, ByVal filterValue As Object) As Boolean
'You need to implement code here to check the value of the item parameter based on your rules,
'along with any criteria provided through the filterValue parameter.
'Remember that the item parameter could be the value of a specific property, or it could be a reference to the actual business object.
'And keep in mind that the filterValue parameter is of type Object, and so it could be any value you’d like, even a complex object.

  Dim result As Boolean = False

 
If item IsNot Nothing AndAlso filterValue IsNot Nothing Then
   
Dim objMyBo As MyBo= DirectCast(item, MyBo)
    result = (objMyBo.Key =
CInt(filterValue))
 
End If

 
Return result
End Function

Joe

Copyright (c) Marimer LLC