Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Predicate in Net 3.5

Reply
Thread Tools

Predicate in Net 3.5

 
 
shapper
Guest
Posts: n/a
 
      11-24-2007
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      11-24-2007
You are probably meaning lambda expressions and LINQ?
http://blogs.msdn.com/vbteam/archive...ion-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


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

"shapper" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPATH Predicate Syntax question Sabba XML 0 05-09-2006 10:48 AM
get position after spec. predicate awebguynow XML 1 01-27-2006 08:42 AM
Predicate by attribute? Jacques XML 2 06-24-2004 11:45 AM
std::stable_sort with predicate woes William Payne C++ 8 04-21-2004 12:37 PM
Help with template find_if predicate mikets C++ 18 01-07-2004 02:36 PM



Advertisments