C#:如何将对象列表转换为该对象的单个属性列表? [英] C#: How to convert a list of objects to a list of a single property of that object?
本文介绍了C#:如何将对象列表转换为该对象的单个属性列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有:
IList<Person> people = new List<Person>();
person 对象具有 FirstName、LastName 和 Gender 等属性.
And the person object has properties like FirstName, LastName, and Gender.
如何将其转换为 Person 对象的属性列表.例如,到一个名字列表.
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 = ???
推荐答案
List<string> firstNames = people.Select(person => person.FirstName).ToList();
并带有排序
List<string> orderedNames = people.Select(person => person.FirstName).OrderBy(name => name).ToList();
这篇关于C#:如何将对象列表转换为该对象的单个属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文