简体   繁体   English

如果我有一个类的集合,如何返回所有类的单个属性的集合?

[英]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.

相关问题 如何将集合中的方法用作其他类的通用方法 - How can I use an method from a collection as an generic method for other classes 如何创建可以迭代的类的集合? - How to create a collection of classes that can be iterated over? 如何将一组IDS集合“分解”为一个DISTINCT IDS集合? - How can I “explode” a collection of collection of IDS into a single collection of DISTINCT IDS? 如何在 C# 中拥有多个派生类型的单个数据集合? - How can I have a single data collection of multiple derived types in C#? 如何处理可观察的类集合 - How to cope with an Observable collection of classes 如何从命名空间中的所有类创建单例集合 - How to create a collection of singletons from all classes in a namespace 如何实现GetAllNodes方法以按级别顺序返回所有节点的集合? - How can I implement a GetAllNodes method to return a collection of all nodes in level-order? 如何绑定具有复杂对象属性的对象集合? - How can I bind my collection of objects with complex object attribute? 如何使用反射从泛型返回所有​​类子类,而不提供特定的泛型类型 - How can I use reflection to return all classes subclassing from a generic, without giving a specific generic type 我怎样才能在 Roslyn 中获得一个方法的所有引用(方法被其他类调用)? - How can I have all references of a method in Roslyn (method is called by other classes)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM