14

Is it possible to search for an object by one of its properties in a Generic List?

Public Class Customer

    Private _id As Integer

    Private _name As String

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set
            _id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set
            _name = value
        End Set
    End Property

    Public Sub New(id As Integer, name As String)
        _id = id
        _name = name
    End Sub

End Class

Then loading and searching

Dim list as new list(Of Customer)

list.Add(New Customer(1,"A")

list.Add(New Customer(2,"B")

How can I return customer object with id =1? Does this have to do with the "Predicate" in Generics?

Note: I am doing this in VB.NET.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saif Khan
  • 18,402
  • 29
  • 102
  • 147

4 Answers4

23

Yes, this has everything to do with predicates :)

You want the Find(Of T) method. You need to pass in a predicate (which is a type of delegate in this case). How you construct that delegate depends on which version of VB you're using. If you're using VB9, you could use a lambda expression. (If you're using VB9 you might want to use LINQ instead of Find(Of T) in the first place, mind you.) The lambda expression form would be something like:

list.Find(function(c) c.ID = 1)

I'm not sure if VB8 supports anonymous methods in the same way that C# 2 does though. If you need to call this from VB8, I'll see what I can come up with. (I'm more of a C# person really :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I am using VB9 On 2.0 and not 3.5 on this project :( I'll give the lambda expression a shot. Thanks! – Saif Khan Oct 14 '08 at 06:24
  • If you're using VB9 but restricted to .NET 2.0, you might want to look at LINQBridge: http://www.albahari.com/nutshell/linqbridge.aspx It's basically a LINQ to Objects implementation targeting .NET 2.0. – Jon Skeet Oct 14 '08 at 06:32
7

Generally you need to use predicates:

list.Add(New Customer(1, "A"))
list.Add(New Customer(2, "B"))

Private Function HasID1(ByVal c As Customer) As Boolean
    Return (c.ID = 1)
End Function

Dim customerWithID1 As Customer = list.Find(AddressOf HasID1)

Or with inline methods:

Dim customerWithID1 As Customer = list.Find(Function(p) p.ID = 1)
Ozgur Ozcitak
  • 10,409
  • 8
  • 46
  • 56
1

You could also overload the equals method and then do a contains. like this

Dim list as new list(Of Customer)

list.Add(New Customer(1,"A")

list.Add(New Customer(2,"B")

list.contains(new customer(1,"A"))

the equals method then would look like this

public overrides function Equals(Obj as object) as Boolean
   return Me.Id.Equals(ctype(Obj,Customer).Id
end Function

Not tested but it should be close enough.

chrissie1
  • 5,014
  • 3
  • 26
  • 26
0

If you are using .NET 3.5 this can be done with LINQ to Objects:

How to: Query an ArrayList with LINQ

If not, in .NET 2.0 you can use the Find method of the list.

The idea is that you will need to provide an method that return true if a property of your object satisfies a certain condition.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aleris
  • 7,981
  • 3
  • 36
  • 42