Predicate in Net 3.5

Discussion in 'ASP .Net' started by shapper, Nov 24, 2007.

  1. shapper

    shapper Guest

    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
     
    shapper, Nov 24, 2007
    #1
    1. Advertising

  2. shapper

    Teemu Keiski Guest

    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


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

    "shapper" <> wrote in message
    news:...
    > 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
     
    Teemu Keiski, Nov 24, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacques

    Predicate by attribute?

    Jacques, Jun 24, 2004, in forum: XML
    Replies:
    2
    Views:
    381
    Jacques
    Jun 24, 2004
  2. awebguynow
    Replies:
    1
    Views:
    365
    George Bina
    Jan 27, 2006
  3. Sabba
    Replies:
    0
    Views:
    636
    Sabba
    May 9, 2006
  4. mikets
    Replies:
    18
    Views:
    1,161
    Chris \( Val \)
    Jan 7, 2004
  5. William Payne

    std::stable_sort with predicate woes

    William Payne, Apr 21, 2004, in forum: C++
    Replies:
    8
    Views:
    1,625
    tom_usenet
    Apr 21, 2004
Loading...

Share This Page