filter ilist

jvcoach23

Centurion
Joined
May 22, 2003
Messages
192
Location
Saybrook, IL
I have a custom class QBank with a public property called QBankID. I
populate the ilist(to QBank). when the iList is populated, is there a way
i can filter on the QBankID property for where it = x.

I could filter this in the database call.. but i'd like to bring back a
larger dataset and only make one call to the database for the data
retrieval, then in code filter for the id i want, work on that.. and than
filter for another id and so on. i'm just trying to limit my db calls. I
could loop through the iList to get the value, just wondering if there is a
better way..

thanks
shannon
 
Unless there are extenuating circumstances, the data server is the best place to filter this data. Unless the data server is slow (xml via http for example) multiple calls to a db should be OK. What type of database are you using?

SQL Server should be able to handle whatever you throw at it; even MS Access can handle multiple queries in a short period of time.
 
It's Sql server..

i was going to have about 80 queries that would have to hit the sql box. i was just looking to optimize things.. if i can do just one query, then filter in code, that would be faster than doing the 80 queries.

is filtering a class on a property hard to do???
 
well.. right now i'm not doing any interface work, this is all just in the background.. logic that will eventually write data to the database. i'm doing it in code because there is some string munipulation that would be much harder for me to do in sql.. So i'm looking for ways to filter the data in the class

thanks
 
Depends on the version you are using for .Net you could do something like
Visual Basic:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim customers As New List(Of Customer)

        customers.Add(New Customer(1, "test"))
        customers.Add(New Customer(2, "Another test"))
        customers.Add(New Customer(3, "test again"))
        customers.Add(New Customer(4, "testing"))
        customers.Add(New Customer(5, "final test"))

        Dim results As List(Of Customer) = customers.FindAll(AddressOf BeginsWithT)

        For Each c1 As Customer In results
            Debug.WriteLine(c1.Name)
        Next
    End Sub

    Private Shared Function BeginsWithT(ByVal c As Customer) As Boolean

        Return c.Name.StartsWith("t")
    End Function


End Class


Public Class Customer

    Public Sub New(ByVal id As Integer, ByVal name As String)
        _Id = id
        _Name = name
    End Sub


    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property


    Private _Id As Integer
    Public Property NewProperty() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property


End Class
The above (contrived) example fills a list with a custom class and then uses a predicate filter (The beginswitht method) to return a filtered list of results.

If you are using .Net 3 or later then Linq could also be used
Visual Basic:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim customers As New List(Of Customer)

        customers.Add(New Customer(1, "test"))
        customers.Add(New Customer(2, "Another test"))
        customers.Add(New Customer(3, "test again"))
        customers.Add(New Customer(4, "testing"))
        customers.Add(New Customer(5, "final test"))

        ' Dim results As List(Of Customer) = customers.FindAll(AddressOf BeginsWithT)

        Dim results = From c In customers Where c.Name.StartsWith("t") Select c


        For Each c1 As Customer In results
            Debug.WriteLine(c1.Name)
        Next
    End Sub

End Class


Public Class Customer

    Public Sub New(ByVal id As Integer, ByVal name As String)
        _Id = id
        _Name = name
    End Sub


    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property


    Private _Id As Integer
    Public Property NewProperty() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property


End Class
 
Back
Top