Find Method on Generic List

Discussion in 'ASP .Net' started by Fresno Bob, Sep 7, 2008.

  1. Fresno Bob

    Fresno Bob Guest

    I have a generic collection of objects and I would like to find the object
    by one of it's properties e.g. I would like something with the functionality
    of something like list.find(Customer.CustomerID = 1). Is there an easy way
    to do this. The stuff with predicates and findall is a little confusing.
    Fresno Bob, Sep 7, 2008
    #1
    1. Advertising

  2. "Fresno Bob" <> wrote in message
    news:...
    >I have a generic collection of objects and I would like to find the object
    >by one of it's properties e.g. I would like something with the
    >functionality of something like list.find(Customer.CustomerID = 1). Is
    >there an easy way to do this. The stuff with predicates and findall is a
    >little confusing.
    >


    You don't state your Language or version. In C# 3:-

    Customer cust = list.Find(c => c.CustomerID == 1);

    In C# 2 you need to bloat the lambda to a delegate:-

    Customer cust = list.Find(delegate(Customer c) { return c.CustomerID ==
    1;});

    I don't do VB so but the principle I'm sure is the same.

    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Sep 7, 2008
    #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. Dotnet Gruven
    Replies:
    1
    Views:
    5,162
    Steven Cheng[MSFT]
    Sep 14, 2005
  2. Replies:
    2
    Views:
    4,059
  3. =?Utf-8?B?dHBhcmtzNjk=?=

    ObjectDataSource could not find a non-generic method error

    =?Utf-8?B?dHBhcmtzNjk=?=, Apr 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    14,537
    =?Utf-8?B?dHBhcmtzNjk=?=
    May 4, 2006
  4. Dennis
    Replies:
    0
    Views:
    1,142
    Dennis
    Jul 1, 2006
  5. mark4asp
    Replies:
    1
    Views:
    507
    mark4asp
    Feb 16, 2007
Loading...

Share This Page