[英]If I have a collection of classes how can I return a collection of a single attribute of all the classes?
Imagine I have 20 People objects. 想象一下,我有20个人物。
IQueryable<Person> People;
How could I return a List of all the Peoples names using a linq query? 如何使用linq查询返回所有民族名称的列表?
IEnumerable<String> names = People.Select(p => p.Name);
This assumes that a the property is of type String and is called Name. 假定a属性为String类型,并且称为Name。
edit: If you specifically want a list, you can add a ToList() call on the end: 编辑:如果您特别想要一个列表,则可以在末尾添加ToList()调用:
List<String> names = People.Select(p => p.Name).ToList();
var names = from person in People select person.Name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.