开发者

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

开发者 https://www.devze.com 2023-01-10 18:17 出处:网络
Say I have a class: public class theclass { public string a; publ开发者_如何转开发ic string b; public string c;

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号