1

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?

1
  • Shouldn't you be riding a sandworm somewhere? grin Commented Aug 6, 2010 at 15:09

3 Answers 3

5
TheClass[] myClasses = GetTheArray();

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

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

Sign up to request clarification or add additional context in comments.

Comments

3

Yes.

IEnumerable<string> bValues = myArray.Select(myClass => myClass.b);

2 Comments

well if he doesn't know Select, he probably doesn't know ToList either, so it's worth including in the example.
Just started fiddling with linq, so I'm still working through things.
2
var valuesForB = yourArray.Select((arrayMember) => arrayMember.b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.