Get Item from Collection by unique ID

Posted by David Murdoch on Stack Overflow See other posts from Stack Overflow or by David Murdoch
Published on 2010-04-27T14:57:23Z Indexed on 2010/04/27 15:03 UTC
Read the original article Hit count: 176

Filed under:
|
|
|

I have a collection of Contacts that inherits from CollectionBase:

public class ContactCollection : CollectionBase{
    //...
}

each contact in the collection has a unique ID:

public class Contact{
    public int ContactID{
        get;
        private set;
    }
    //...
}

I think what I would like to do is something like the following:

// get the contact by their unique [Contact]ID
Contact myPerson = Contact.GetContactById(15);

// get all contacts for the customer
ContactCollection contacts = customer.GetContacts();

// replaces the contact in the collection with the 
// myPerson contact with the same ContactID.
contacts.ReplaceAt(myPerson);

// saves the changes to the contacts and the customer
// customer.Save();

There is probably a better way...if so, please suggest it.

© Stack Overflow or respective owner

Related posts about c#3.0

Related posts about c#