Filter a Typedlist

Posts   
 
    
shahab
User
Posts: 4
Joined: 24-Feb-2006
# Posted on: 21-Mar-2006 06:01:07   

How can I filter a trypedlist to bring only fields related a ID . For example I need to bring in all the fields from the typedlist which has ID=1 Here is the code I am using.

    Dim dv As DataView
    Dim RETL As New RequestAndProfitCentreAccountTypedList
    Dim adapter As New DataAccessAdapter

    adapter.FetchTypedList(RETL)

    dv = RETL.DefaultView
    dv.RowFilter = "AccountID=" & ID

    adapter.CloseConnection()

Thanks in advance Shahab

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Mar-2006 07:59:58   

I see that you try to filter the results after being fetched.

If you want to filter the results on the fetch process, you might want to do the folowing:


Dim RETL As New RequestAndProfitCentreAccountTypedList  
        
Dim filter As New PredicateExpression
filter.Add(PredicateFactory.CompareValue(RequestAndProfitCentreAccountFieldIndex.Id, ComparisonOperator.Equal, SomeID))

Dim Bucket As New RelationPredicateBucket
Bucket.PredicateExpression.Add(filter)

Dim adapter As New DataAccessAdapter
adapter.FetchTypedList(RETL.GetFieldsInfo(), RETL, Bucket, 0, sorter, False)

If you want to get all the records then filter them later using a DataView Then just skip the Bucket filtering from the above code and call the FetchTypedList as follows:

adapter.FetchTypedList(RETL.GetFieldsInfo(), RETL, Nothing, 0, sorter, False)

and use the DefaultView RowFilter as you did.

P.S. the DataAccessAdapter close the connection on its own unless you passes true to the constructor to keep the connection open, then you should call the CloseConnection method to close it at the end.

  • Please excuse any VB syntax mistakes
shahab
User
Posts: 4
Joined: 24-Feb-2006
# Posted on: 22-Mar-2006 01:42:25   

Thanks I tried the code but I keep getting this error:

Message "Object reference not set to an instance of an object." String

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Mar-2006 07:35:29   

The above code was for demonestration it was not tested, so please tell me which line gave that error and a possible stack trace would be good to identify the error.

I see I have included a "sorter" parameter that was not defined in the above code, you may want to pass "Nothing" in this case.