0

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?

2 Answers 2

4

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();
Sign up to request clarification or add additional context in comments.

1 Comment

it seems what i'm looking for. i'll have a try. ta
0

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

2 Comments

i'm sorry but i dont really understand with that
Ah It is same like answer above, but using linq query, for me it's self explaining

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.