spondbob
spondbob

Reputation: 1633

copy list of objects into another list of objects

I have a question here. Lets say I have these two classes

class Person
{
    int person_id;
    string name;
    Address address;
}

class Address
{
    int address_id;
    string street;
}

and I have list of Person data in List<Person> people = new List<Person>();.

Now I want to copy all of the address in people to a new address list List<Address> addresses = new List<Address>();. How can I achieve this?

Upvotes: 0

Views: 744

Answers (2)

quirell
quirell

Reputation: 255

(From p in people select p.Address).ToList() Is that it?

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

You want to do a projection which means take the objects and transform them into another form:

List<Address> addresses = people.Select(p => p.Address).ToList();

Upvotes: 4

Related Questions