Getting all of the values for a property for a list of objects
Say I have a class:
public class theclass
{
public string a;
publ开发者_如何转开发ic 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)
Yes.
IEnumerable<string> bValues = myArray.Select(myClass => myClass.b);
var valuesForB = yourArray.Select((arrayMember) => arrayMember.b);
精彩评论