Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

linq - C#: How to convert a list of objects to a list of a single property of that object?

Say I have:

IList<Person> people = new List<Person>();

And the person object has properties like FirstName, LastName, and Gender.

How can I convert this to a list of properties of the Person object. For example, to a list of first names.

IList<string> firstNames = ???
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
List<string> firstNames = people.Select(person => person.FirstName).ToList();

And with sorting

List<string> orderedNames = people.Select(person => person.FirstName).OrderBy(name => name).ToList();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...