简体   繁体   中英

Getting all of the values for a property for a list of objects

Say I have a class:

public class theclass
{
  public string a;
  public string b;
  public string c;
}

Yes. It's a bad class. Moving on. Say I have a 100 value array of this class. Is there a quick way with linq to get a list of strings with all of the values of b for the contents of the array?

TheClass[] myClasses = GetTheArray();

List<string> = myClasses.Select(c => c.A).ToList();

(I changed your class/property names to PascalCase, as per coding standard convention )

是。

IEnumerable<string> bValues = myArray.Select(myClass => myClass.b);
var valuesForB = yourArray.Select((arrayMember) => arrayMember.b);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM