Create List with only one class member

c#, list

Solution

Use LINQ:

public List<string> GetAllNames()
{
    return MyDataList.Select(i => i.name).ToList();
}

Problem

I have a list of this class: ``` public class Data { public string name {get; set;} public int width {get; set;} } ``` And I want to make a method that return a list of only the name property. Like this: ``` public List<string> GetAllNames() { return MyDataList<name>.ToList(); } ``` So, if I have this list: - `name` = Jon - `width` = 10 - `name` = Jack - `width` = 25 I want the following list: - `name` = Jon - `name` = Jack Is it possible?

Original source