I have a following class:
public class Person
{
public string FirstName = string.Empty;
public string LastName = string.Empty;
}
and list of that class:
List<Person> listOfPeople = new List<Person>();
How do I get list FirstNames into string or string list variable from listOfPeople without using foreach and looping through it? I want to get something like:
string firstNames= "Bob, Jack, Bill";
A simple
Select
combined withstring.Join
will give you what you want.Depending on what version of .Net you are using you might also need to add a
ToArray
after theSelect
as the overload ofstring.Join
that takes aIEnumerable<string>
was not added until .Net 4.On a side note consider turning your public fields into properties.