Predicate in Net 3.5

S

shapper

Hello,

I have a List(Of MyClass).
MyClass has two properties: Name and City.

I want to find, without using a loop, if there is a item where name =
NameParameter.

I know that I can use a Predicate but in .NET 2.0 Predicates do not
accepted Parameters.

Because of that I used a Predicate Wrapper.

With .NET 3.5 I know this has changed and now it is much easier to do
something like this.

However, I can't find any example of it.

Can someone, please, help me out?

Thanks,
Miguel
 
T

Teemu Keiski

You are probably meaning lambda expressions and LINQ?
http://blogs.msdn.com/vbteam/archive/2007/09/11/lambda-expressions-and-expression-trees.aspx

Here's an example

Public Class MySampleClass

Public Sub New(ByVal Name As String, ByVal City As String)
Me.Name = Name
Me.City = City
End Sub

Private _name As String
Private _city As String

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

Public Property City() As String
Get
Return _city
End Get
Set(ByVal value As String)
_city = value
End Set
End Property

End Class



Dim l As New List(Of MySampleClass)
l.Add(New MySampleClass("Karl", "NY"))
l.Add(New MySampleClass("Mike", "NY"))
l.Add(New MySampleClass("Lars", "LA"))

Dim lookingFor As String = "NY"

Dim guys = From person In l _
Where person.City = lookingFor _
Select person

For Each p As MySampleClass In guys
Response.Write(p.Name)
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top